Siwa Posted November 16, 2020 Share Posted November 16, 2020 (edited) Hi autoit community, I have a strange problem, I have a loop which checks something of a webpage created by IE, I want the code to stop until my desired condition is met( JavaScript-page fully loaded), but the loop continues forever despite that my condition is met. Here is my DO...UNTIL loop I am stuck. Total numbers =... ;Some java query which returns a number, I included the console. $i = 0 Do $i += 1 $Specific =... ;Some java query which returns a number, I included the console. $Specific_Numers = StringRegExp($Specific, '\d+(?:,\d+)?', 1) Sleep(10) ConsoleWrite (@CRLF & $i & " " & "Total numbers =" & $Total_Numbers & " Smaller size is =" & $Specific_Numers[0] ) Until $Total_Numbers >$Specific_Numers[0] And here is part of my console : Spoiler Total numbers = 1 Total numbers =196 Smaller size is =3 2 Total numbers =196 Smaller size is =3 3 Total numbers =196 Smaller size is =3 4 Total numbers =196 Smaller size is =3 5 Total numbers =196 Smaller size is =3 6 Total numbers =196 Smaller size is =3 7 Total numbers =196 Smaller size is =3 8 Total numbers =196 Smaller size is =3 9 Total numbers =196 Smaller size is =3 10 Total numbers =196 Smaller size is =3 11 Total numbers =196 Smaller size is =3 12 Total numbers =196 Smaller size is =3 13 Total numbers =196 Smaller size is =3 14 Total numbers =196 Smaller size is =3 15 Total numbers =196 Smaller size is =3 16 Total numbers =196 Smaller size is =3 17 Total numbers =196 Smaller size is =3 18 Total numbers =196 Smaller size is =3 19 Total numbers =196 Smaller size is =3 20 Total numbers =196 Smaller size is =3 21 Total numbers =196 Smaller size is =3 22 Total numbers =196 Smaller size is =3 23 Total numbers =196 Smaller size is =3 24 Total numbers =196 Smaller size is =3 25 Total numbers =196 Smaller size is =3 26 Total numbers =196 Smaller size is =3 27 Total numbers =196 Smaller size is =3 28 Total numbers =196 Smaller size is =3 29 Total numbers =196 Smaller size is =3 30 Total numbers =196 Smaller size is =3 31 Total numbers =196 Smaller size is =3 32 Total numbers =196 Smaller size is =3 33 Total numbers =196 Smaller size is =3 34 Total numbers =196 Smaller size is =3 35 Total numbers =196 Smaller size is =3 36 Total numbers =196 Smaller size is =3 37 Total numbers =196 Smaller size is =3 38 Total numbers =196 Smaller size is =3 39 Total numbers =196 Smaller size is =3 40 Total numbers =196 Smaller size is =3 41 Total numbers =196 Smaller size is =3 42 Total numbers =196 Smaller size is =3 43 Total numbers =196 Smaller size is =3 44 Total numbers =196 Smaller size is =3 45 Total numbers =196 Smaller size is =3 46 Total numbers =196 Smaller size is =3 47 Total numbers =196 Smaller size is =3 48 Total numbers =196 Smaller size is =3 49 Total numbers =196 Smaller size is =3 50 Total numbers =196 Smaller size is =3 I know it is a very basic question, but I'm stuck at this code, and I guess I need some experts opinions for this. Edited November 16, 2020 by Siwa Link to comment Share on other sites More sharing options...
Nine Posted November 16, 2020 Share Posted November 16, 2020 Might be because, you are comparing string with number Siwa, JockoDundee and GokAy 2 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
pseakins Posted November 16, 2020 Share Posted November 16, 2020 32 minutes ago, Siwa said: Until $Total_Numbers >$Specific_Numers[0] Or... Perhaps $Total_Numbers is never greater than $SpecificNumers[0]. Perhaps your expression should be >= rather than > Until $Total_Numbers >= $Specific_Numers[0] JockoDundee and Siwa 1 1 Phil Seakins Link to comment Share on other sites More sharing options...
JockoDundee Posted November 16, 2020 Share Posted November 16, 2020 23 minutes ago, Nine said: Might be because, you are comparing string with number Nine is correct. Try: $Specific_Numers = Number(StringRegExp($Specific, '\d+(?:,\d+)?', 1)) btw, @Siwa, it could have been much worse; had the TotalNumbers been 396 instead of 196, the loop would have ended, and the bug might not have been detected right away... Siwa and GokAy 2 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
pseakins Posted November 16, 2020 Share Posted November 16, 2020 Also, I suspect that $Specific_Numers is a typo and you actually meant to type $Specific_Numbers. I recommend you fix this to avoid confusion and mysterious bugs in the future. Siwa 1 Phil Seakins Link to comment Share on other sites More sharing options...
pseakins Posted November 16, 2020 Share Posted November 16, 2020 From the help; "In AutoIt there is only one datatype called a Variant. if you try and multiply two variants they will be treated as numbers. If a string is used as a number, an implicit call to Number() function is done. So if it doesn't contain a valid number, it will be assumed to equal 0." In @Siwa's expression, $Total_Numbers has already been assigned a numeric value therefore $Specific_Numers will be automatically assigned a numeric value. Therefore, this isn't his issue. Siwa 1 Phil Seakins Link to comment Share on other sites More sharing options...
JockoDundee Posted November 17, 2020 Share Posted November 17, 2020 1 hour ago, pseakins said: In @Siwa's expression, $Total_Numbers has already been assigned a numeric value therefore $Specific_Numers will be automatically assigned a numeric value. Disagree. Here, then: Local $iNumber $iNumber=50 ConsoleWrite("iNumber=50" &@CRLF) If IsNumber($iNumber) Then ConsoleWrite("Its a Number" &@CRLF) Else ConsoleWrite("Its Not a Number" &@CRLF) EndIf If IsString($iNumber) Then ConsoleWrite("Its a String" &@CRLF) Else ConsoleWrite("Its not a String" &@CRLF) EndIf ConsoleWrite(@CRLF) $iNumber="50" ConsoleWrite("iNumber=""50""" &@CRLF) If IsNumber($iNumber) Then ConsoleWrite("Its a Number" &@CRLF) Else ConsoleWrite("Its Not a Number" &@CRLF) EndIf If IsString($iNumber) Then ConsoleWrite("Its a String" &@CRLF) Else ConsoleWrite("Its not a String" &@CRLF) EndIf iNumber=50 Its a Number Its not a String iNumber="50" Its Not a Number Its a String Siwa 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
GokAy Posted November 17, 2020 Share Posted November 17, 2020 Hey all, I did face an issue when a number was passed to a script as an argument, it would fail to interpret as a number, and had to convert to a number explicitly. Maybe something I did wrong, idk. (Just an AutoIt nooby after all ) Siwa 1 Link to comment Share on other sites More sharing options...
JockoDundee Posted November 17, 2020 Share Posted November 17, 2020 1 minute ago, GokAy said: Hey all, I did face an issue when a number was passed to a script as an argument, it would fail to interpret as a number, and had to convert to a number explicitly. Maybe something I did wrong, idk. (Just an AutoIt nooby after all ) Can you recall the actual code? Siwa 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
pseakins Posted November 17, 2020 Share Posted November 17, 2020 15 minutes ago, JockoDundee said: Disagree. Here, then: What is your point? Yes, that is exactly how AutoIt works. However, @Siwaalready told us that his variable $Total_Numbers is a number when he defined it in his pseudocode;- "Total numbers =... ;Some java query which returns a number, I included the console." It then follows that the other side of his expression will be automatically "Number() converted, as defined in AutoIt help. So, unless he is actually comparing strings (and we don't know that because we haven't seen all his code) his expression may fail without ">=". Siwa 1 Phil Seakins Link to comment Share on other sites More sharing options...
GokAy Posted November 17, 2020 Share Posted November 17, 2020 (edited) Yeah, Maybe Number() would be enough, need it to be an integer though. Example would be: 2 NO-OVERWRITE Global $g_bGUI_MODE = True if $CmdLine[0] = 0 Then ; Script started by User (no params needed) ; $g_iSampleCopies ; $g_sCopyMethod Elseif $CmdLine[0] = 2 Then ; Script started by Excel (params required) $g_iSampleCopies = Int($CmdLine[1], $NUMBER_32BIT) $g_sCopyMethod = $CmdLine[2] ; Check if $g_iSampleCopies is an integer, terminate if not, then terminate if less than 1 if not IsInt($g_iSampleCopies) then Exit if $g_iSampleCopies < 1 then Exit ; Check if $g_sCopyMethod is a valid parameter, terminate if not if StringInStr("DELETE OVERWRITE NO-OVERWRITE", $g_sCopyMethod) = 0 then Exit ; If all params are correct then set GUI_Mode = False $g_bGUI_MODE = False Else ; Wrong number of params, abort!!! Exit EndIf Edited November 17, 2020 by GokAy Siwa 1 Link to comment Share on other sites More sharing options...
JockoDundee Posted November 17, 2020 Share Posted November 17, 2020 26 minutes ago, pseakins said: What is your point? Yes, that is exactly how AutoIt works. However, @Siwaalready told us that his variable $Total_Numbers is a number when he defined it in his pseudocode;- "Total numbers =... ;Some java query which returns a number, I included the console." It then follows that the other side of his expression will be automatically "Number() converted, as defined in AutoIt help. So, unless he is actually comparing strings (and we don't know that because we haven't seen all his code) his expression may fail without ">=". I don’t see how you can say that - he prints both variables right before the evaluation. ConsoleWrite (@CRLF & $i & " " & "Total numbers =" & $Total_Numbers & " Smaller size is =" & $Specific_Numers[0] ) Until $Total_Numbers >$Specific_Numers[0] And the output shows them as 196 and 3 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
pseakins Posted November 17, 2020 Share Posted November 17, 2020 10 minutes ago, JockoDundee said: And the output shows them as 196 and 3 To be honest, I hadn't even noticed the hidden console output. Reviewing @Siwa's code I see that he is performing StringRegExp on something he is saying is a number. Clearly the value he is working with is actually a string. This then implies that, probably, both his variables are strings and of course "196" is LOWER than "3" in a string comparison. @Siwa You need to perform a Number() conversion on one or both of the variables in your comparison expression. Only one is actually necessary as the other one will be converted by implication. Here is the corrected code. Note I have left in the ">=" rather than ">". Until Number($Total_Numbers) >= $Specific_Numers[0] Siwa 1 Phil Seakins Link to comment Share on other sites More sharing options...
Siwa Posted November 17, 2020 Author Share Posted November 17, 2020 Thanks for your help, and in depth explanations guys. As @pseakins said, the query returned a string, and that was my main problem. Those two variables were reading the same text inside a JavaScript webpage, the first variable read the whole total of incidences, and the second variable reads it after a search is done to lower the numbers and indicating that actually a search is done. The confusing part for me was, that the first number was read by autoit as a number, and when I put a is number if statement it accepted my statement. But the strange part was autoit was not accepting my second variable as a number, and @GokAy was right, I had to declare it as a number with Number(). Link to comment Share on other sites More sharing options...
GokAy Posted November 17, 2020 Share Posted November 17, 2020 15 minutes ago, Siwa said: But the strange part was autoit was not accepting my second variable as a number, and @GokAy was right, I had to declare it as a number with Number(). Actually, @Nine is the one that first mentioned it, and @JockoDundee gave the function. Siwa 1 Link to comment Share on other sites More sharing options...
pseakins Posted November 18, 2020 Share Posted November 18, 2020 16 hours ago, Siwa said: The confusing part for me was, that the first number was read by autoit as a number Incorrect, the number was read by AutoIt as a string. A string literal representation of a number. 16 hours ago, Siwa said: and when I put a is number if statement it accepted my statement. AutoIt will accept the comparison statement regardless of whether the variables are strings or numbers or any other datatype. When it comes to execution AutoIt then figures out what kind of Variants it is dealing with and then processes the statement accordingly. So, to reiterate. 1. Both the variables were interpreted as strings, 2. The Until statement failed because it was doing a string comparison when you intended it to be a numeric comparison. 3. At least one of the variables needed to be Number() converted in order to force the comparison to be numeric. 4. It doesn't matter when the Number() conversion is performed - It can be within the Until expression, or it could have be done earlier on as @JockoDundee suggested upon the output of the StringRegExp expression. 5. If it were done earlier then doing it again within the Until expression would be redundant and unnecessary. Personally, I think it makes the current code easier to understand if the conversion is performed during the Until expression. However, if the variables were to be accessed separately, in other parts of code, yet to be written, it would be better practice to convert BOTH variables during the initial assignment. Maybe you should do this anyway so there is a clear understanding that you are working with number variants. To reinforce this I recommend using a naming convention where the first letters of the variable name define its type. Eg., $iTotalNumbers and $aiSpecificNumbers. Naming conventions are discussed here https://www.autoitscript.com/wiki/Best_coding_practices Having said that, I think it's still better to use $i an $j in loops and $x for a temporary variable. Phil Seakins Link to comment Share on other sites More sharing options...
JockoDundee Posted November 18, 2020 Share Posted November 18, 2020 (edited) My summary of accreditation, IMHO 1) Nine said “you’re comparing a string with a number”. Although, technically this was inaccurate, since actually it was a string compared to a string, it was the essence of the error and the first response as well, and therefore as is typical, earns 50%. 2) I said “Nine is correct.” Affirmations of true statements at best earn 25% of the original statement, which in this case was awarded 50% - therefore it earns a quarter of a half or 12.5%. 3) I posted some code purporting to be a solution. Unfortunately, I overlooked that StringRegExp returns an array, therefore my Number() function would have an unknown, but certainly unhelpful effect if applied to the array directly. Still, with a slight change it would have work - therefore I award it 2.5% 4) pseakins thought that the loop condition was not being reached, causing the infinite loop. He had missed the output listing, which was hidden under a reveal - therefore I award him the nominal 5% “good faith” effort. 5) pseakins insists that there is no number/string typing issue, because he hadn’t seen the console output. 6) I post a pedantic string/numeric example, because I had seen the output, yet still earning 0% for irrelevance. 7) pseakins looks at the output after I mention it, he concludes that both variables must be strings, since if even one is numeric, then the evaluation is numeric. totals: Nine: 50% pseakins: 35% me: 15% Edited November 18, 2020 by JockoDundee GokAy, Siwa and pseakins 1 2 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
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