michaelslamet Posted January 4, 2014 Posted January 4, 2014 Not sure how to do this: I'm going to pull out data from MySQL database sorted on username and save it to a variable, like this: Local $data = "" $data &= "user1 :: 1" & @CRLF $data &= "user1 :: 0" & @CRLF $data &= "user1 :: 1" & @CRLF $data &= "user1 :: 0" & @CRLF $data &= "user1 :: 1" & @CRLF $data &= "user1 :: 1" & @CRLF $data &= "user1 :: 1" & @CRLF $data &= "user2 :: 1" & @CRLF $data &= "user2 :: 0" & @CRLF $data &= "user2 :: 1" & @CRLF $data &= "user2 :: 0" & @CRLF $data &= "user2 :: 1" & @CRLF $data &= "user2 :: 0" & @CRLF $data &= "user2 :: 1" & @CRLF $data &= "user2 :: 1" & @CRLF $data &= "user2 :: 1" & @CRLF $data &= "user2 :: 1" & @CRLF $data &= "user2 :: 0" & @CRLF $data &= "user3 :: 1" & @CRLF $data &= "user3 :: 1" & @CRLF $data &= "user3 :: 1" & @CRLF $data &= "user3 :: 1" & @CRLF $data &= "user3 :: 0" & @CRLF From above data, I would like my script to generate a report like this: User1 Total record = 7 Percentage 1 versus 0 = 5 / 7 x 100% = 71.42% User2 Total record = 11 Percentage 1 versus 0 = 7 / 11 x 100% = 63.63% User3 Total record = 5 Percentage 1 versus 0 = 4 / 5 x 100% = 80.00% Of course "user1", "user2", "user3" could be anything like "Bill", "Susan" and not limited to 3 users. I am even not sure should I pull the MySQL data and save it to a variable as a long string (like above) or as an array, or should I use MySQL command to count total record for each user. Totally lost on how to do it.
jchd Posted January 4, 2014 Posted January 4, 2014 Use SQL to grab information in arrays and AutoIt code to format it the way you want. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
michaelslamet Posted January 5, 2014 Author Posted January 5, 2014 (edited) Use SQL to grab information in arrays and AutoIt code to format it the way you want. I solved it this way. What are another ways that is faster, more efficient and shorter code? expandcollapse popup#include <array.au3> Local $sData = "" $sData = "user1 :: 1" & @CRLF $sData &= "user1 :: 0" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user1 :: 0" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user1 :: 1" & @CRLF $sData &= "user2 :: 1" & @CRLF $sData &= "user2 :: 0" & @CRLF $sData &= "user2 :: 1" & @CRLF $sData &= "user2 :: 0" & @CRLF $sData &= "user2 :: 1" & @CRLF $sData &= "user2 :: 0" & @CRLF $sData &= "user2 :: 1" & @CRLF $sData &= "user2 :: 1" & @CRLF $sData &= "user2 :: 1" & @CRLF $sData &= "user2 :: 1" & @CRLF $sData &= "user2 :: 0" & @CRLF $sData &= "user3 :: 1" & @CRLF $sData &= "user3 :: 1" & @CRLF $sData &= "user3 :: 1" & @CRLF $sData &= "user3 :: 1" & @CRLF $sData &= "user3 :: 0" & @CRLF Local $aData = StringSplit($sData, @CRLF, 3) Local $sSaveUser = "", $sOut = "", $i0, $i1 = 0 For $a = 0 To UBound($aData) - 1 If $sSaveUser = StringLeft($aData[$a], StringInStr($aData[$a], ' ::') - 1) Then If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then $i1 += 1 Else $i0 += 1 EndIf Else If $a <> 0 Then $sOut &= $sSaveUser & @CRLF & "sum of 1 = " & $i1 & @CRLF & "sum of 0 = " & $i0 & @CRLF & @CRLF $sSaveUser = StringLeft($aData[$a], StringInStr($aData[$a], ' ::') - 1) $i0 = 0 $i1 = 0 If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then $i1 += 1 Else $i0 += 1 EndIf EndIf If StringLen($aData[$a]) = 0 Then ExitLoop Next ConsoleWrite($sOut) The result is: user1 sum of 1 = 9 sum of 0 = 2 user2 sum of 1 = 7 sum of 0 = 4 user3 sum of 1 = 4 sum of 0 = 1 Edited January 5, 2014 by michaelslamet
jchd Posted January 5, 2014 Posted January 5, 2014 What I said, but I'm not familiar enough with MySQL to provide code. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
michaelslamet Posted January 5, 2014 Author Posted January 5, 2014 What I said, but I'm not familiar enough with MySQL to provide code. In fact, I solved it using your advice Nevermind about the MySQL part. I wonder about the "AutoIT part". Although my code run just fine, I'm not confident it's the best way to do it. That is why I'm asking for advices, suggestions, codes to tweak it. So, please lead me to the light
jchd Posted January 5, 2014 Posted January 5, 2014 Post your code please This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
michaelslamet Posted January 5, 2014 Author Posted January 5, 2014 Post your code please My code is on post #3, please scroll down to see it
kylomas Posted January 5, 2014 Posted January 5, 2014 ms, Your code runs 100,000 entries in 1.2 seconds. Seems pretty fast to me Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
michaelslamet Posted January 5, 2014 Author Posted January 5, 2014 ms, Your code runs 100,000 entries in 1.2 seconds. Seems pretty fast to me Good to hear that As you might see, I learn from you from >this post and borrow the logic (and the vars!) Anyway, I always wonder, cant we shorten this code: If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then $i1 += 1 Else $i0 += 1 EndIf To only 1 line, something like: If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then $i1 += 1 Else $i0 += 1 EndIf It doesn't work with 3.8.8.1, but why? Why dont we make it run at 3.3.10 ? Because writing it that way is a bad practice?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now