ChiDragon Posted October 16, 2005 Share Posted October 16, 2005 (edited) I encountered this issue when I was pulling some numbers from text files and doing some operations on them. Numbers from text files, and numbers entered manually with quotes around them, seem to work in most mathematical operations, but "greater than" and "less than" comparisons between strings with a different number of digits invariably produce opposite results than they mathematically should. Example: If "100000000000" < "9" Then MsgBox (0, "?", "100000000000 < 9") EndIf If "9" > "100000000000" Then MsgBox (0, "?", "9 > 100000000000") EndIf I solved my problem with the numbers from text files producing results like this by doing Number() on the variables, but I can't help but wonder why this is. Doing the same operations as above but without the quotes around the numbers gives the correct results. I guess AutoIt evaluates numbers as strings differently than just plain numbers, but why? Edited October 16, 2005 by ChiDragon Link to comment Share on other sites More sharing options...
bluebearr Posted October 16, 2005 Share Posted October 16, 2005 In string comparisions of numbers, the strings are compared from left to right - i.e., no leading zeroes are added to the string. Thus, since 9 is greater than 1, "9" is considered greater than "100000000000". If you run this code, you see no Msgboxes: If "700000000000" < "6" Then MsgBox (0, "?", "700000000000 < 6") EndIf If "6" > "700000000000" Then MsgBox (0, "?", "6 > 700000000000") EndIf This behavior is consistent with other languages and programs. For example, if you force strings in Excel cells, you will get similar answers. BlueBearrOddly enough, this is what I do for fun. Link to comment Share on other sites More sharing options...
Valik Posted October 16, 2005 Share Posted October 16, 2005 bluebearr, thats not correct. They are compared lexicographically. The character which appears early in the character set is less than the other. For example, a < b not because a comes before b in the English language but because a is the 97th character in the ASCII character set and b is the 98th character. Under the ASCII character set, the following is true: a < b < c However, it's possible (though worthless) to create a character set in which the following might be true: a > b < c The same holds true for strings which the OP shows because they are interpreted as strings and not numbers. Link to comment Share on other sites More sharing options...
ChrisL Posted October 16, 2005 Share Posted October 16, 2005 (edited) I encountered this issue when I was pulling some numbers from text files and doing some operations on them. Numbers from text files, and numbers entered manually with quotes around them, seem to work in most mathematical operations, but "greater than" and "less than" comparisons between strings with a different number of digits invariably produce opposite results than they mathematically should.Example:If "100000000000" < "9" Then MsgBox (0, "?", "100000000000 < 9") EndIf If "9" > "100000000000" Then MsgBox (0, "?", "9 > 100000000000") EndIfI solved my problem with the numbers from text files producing results like this by doing Number() on the variables, but I can't help but wonder why this is. Doing the same operations as above but without the quotes around the numbers gives the correct results. I guess AutoIt evaluates numbers as strings differently than just plain numbers, but why?I think If I understand what your asking then I had something similar.. I think it was because I was pulling the number from a text file it was read as a string and not a number. If you make sure the string is a number - $Var = StringIsDigit ( "string" ) and $Nvar = Number ($Var) then you should be sure $Nvar is a number and not a string..Someone correct me if I have got this totally wrong Edited October 16, 2005 by ChrisL [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
bluebearr Posted October 16, 2005 Share Posted October 16, 2005 The character which appears early in the character set is less than the other.I apologize for my technically incorrect shorthand. BlueBearrOddly enough, this is what I do for fun. Link to comment Share on other sites More sharing options...
herewasplato Posted October 16, 2005 Share Posted October 16, 2005 I also encountered this issue (a little over a year ago). I do not understand exactly how AutoIt3 works. I would like to make some statements and ask that they be corrected - if needed. I will try to use the correct terms... feel free to correct that too. This is a slight rewording from the AutoIt3 manual: In AutoIt3 there is only one datatype called a Variant. A variant can contain numeric or string data. AutoIt3 decides how to use the data depending on the situation. ChiDragon states, ...I was pulling some numbers from text files... Assuming it was pulled with code like this: $line = FileReadLine($file) Statement 1: $line contains string data at this point - even if the value is 60 or abc. Using this example:$N_var60 = 60 $N_var7 = 7 $S_var60 = "60" $S_var7 = "7" MsgBox(0,"",$N_var60 + $N_var7);returns 67 MsgBox(0,"",$S_var60 + $S_var7);returns 67 MsgBox(0,"",$N_var60 > $N_var7);returns 1 true MsgBox(0,"",$S_var60 > $S_var7);returns 0 false Statement 2: AutoIt3 decided to use the string data in $S_var60 and $S_var7 as numeric data for the + operator. Statement 3: AutoIt3 decided to use the string data in $S_var60 and $S_var7 as string data for the >operator. Statement 4: ChiDragon was surprised by AutoIts decision in described in Statement 3. Question 1: Is this behavior consistent with other languages and programs? Specifically, do other languages automatically: use string data as if it were numeric data for the + operator and use string data as string data for the > operator? Just asking... Im not saying anything against AutoIt3. Question 2: Does any one see any part of the 4 statements that need correcting (or any other part of the post)? Thanks for you time. BTW - This was not an attempt to hijack the thread... only to clarify ChiDragon's "...why this is." in the form of statements and questions to those that can explain it all better than I. [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Valik Posted October 16, 2005 Share Posted October 16, 2005 There is no logical context for + in AutoIt when dealing with strings. There is a logical context when dealing with numbers. AutoIt promotes strings to numbers when it encounters + because there is nothing it can do while they are strings but it can do something if they are numbers. The < operator, however, has a logical context with both numbers and strings so no promotion is performed. It's a perfectly reasonable operation to want to compare a string of numbers using < but have the string treated as a string and not a number. However, there is no defined operation for using + on two strings so a promotion occurs. Link to comment Share on other sites More sharing options...
herewasplato Posted October 16, 2005 Share Posted October 16, 2005 Well put and thanks. Wish I had been that concise. Hmmm... I wonder if my management considers it a promotion to treat me like a number. :-) [size="1"][font="Arial"].[u].[/u][/font][/size] 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