if(VarContainingString =/!= OtherVarContainingString) Bug

Report problems with documented functionality
Hellbent
Posts: 764
Joined: 23 Sep 2017, 13:34

if(VarContainingString =/!= OtherVarContainingString) Bug

17 Oct 2019, 19:52

I think that this should speak for it's self.

Code: Select all

Var1:="+1"
Var2:="1"

msgbox,% Var1 "`n" Var2  "`nClearly they are different."

if( Var1 != Var2 )
     msgbox, they are different but you won't get to see this message

Var1 = +1
Var2 = 1
msgbox,% Var1 "`n" Var2  "`nClearly they are different."

if( Var1 != Var2 )
     msgbox, they are different but you won't get to see this message
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: if(VarContainingString =/!= OtherVarContainingString) Bug

17 Oct 2019, 20:59

In order to perform string comparison, try ==

Code: Select all

Var1 = +1
Var2 = 1
msgbox,% Var1 "`n" Var2  "`nClearly they are different."

if( Var1 == Var2 )
     msgbox, they are different and you will get to see this message
gregster
Posts: 3379
Joined: 30 Sep 2013, 06:48

Re: if(VarContainingString =/!= OtherVarContainingString) Bug

17 Oct 2019, 20:59

I think that's just AHK's usual and long-established way of smartly handling variables. An if-comparison with its = equal-operator causes these strings to be seen as numbers, as long as you don't force it to see them as strings...
https://www.autohotkey.com/docs/Concepts.htm#caching wrote:Caching

Although a variable is typically thought of as holding a single value, and that value having a distinct type (string, number or object), AutoHotkey automatically converts between numbers and strings in cases like myString + 1 and MsgBox %myNumber%. As these conversions can happen very frequently, whenever a variable is converted, the result is cached in the variable.

In effect, a variable can contain both a string and a number simultaneously. Usually this just improves the script's performance with no down-sides, but if a variable contains both a number and a string, is it number, or is it a string? This ambiguity causes unexpected behavior in at least two cases:[...]
And it sometimes causes just a bit of confusion :)

Code: Select all

Var1:="+1"
Var2:="1"

msgbox,% Var1 "`n" Var2  "`nClearly they are different."


if( Var1 "" != Var2 "" )
{
    msgbox, % "yes, they are different strings " Var1 " vs. " Var2 
	msgbox, % "but if you try to calculate with them:  " Var1 + Var2 " , " Var1 - Var2
	msgbox, % "or do a comparison (for example, but not only with if), you get:  " (Var1 = Var2) "   (true)"
	msgbox, % "now compare strings:  " (Var1 "" = Var2 "") "   (false)"
}
But...
https://www.autohotkey.com/docs/Variables.htm#equal wrote:A quoted literal string such as "55" is always considered non-numeric in this context.

Edit: I think wolf_II's example suffers from the same caching logic as the original code... :think:

Edit2: Another example

Code: Select all

Var1 :="+1"
msgbox % Var1 + var1 
msgbox % Var1 "" + var1
Hellbent
Posts: 764
Joined: 23 Sep 2017, 13:34

Re: if(VarContainingString =/!= OtherVarContainingString) Bug

17 Oct 2019, 21:29

@wolf_II
Thanks Wolf but that gets the same result, they evaluate as equal.


@gregster

Thanks. Seems a bit odd, but it is how it is.

I was going to use something like this

Code: Select all

if((Var1!=Var2&&Var2!="")||(Var1=Var2&&Strlen(Var1)!=Strlen(Var2)))
But

Code: Select all

if(Var1 "" != Var2 "" && Var2!="")
Is a better solution.


***Edit***
Seems a bit odd, but it is how it is.
On second thought, it's not odd when considering reading from a file.
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: if(VarContainingString =/!= OtherVarContainingString) Bug

18 Oct 2019, 19:26

The difference between 1 and +1 is 0. They are equal. 0x1, 001 and 1.0 are also equal to 1, but all present differently in MsgBox.

For backward-compatibility and because the legacy syntax was string-oriented, AutoHotkey v1 does not make a strong distinction between pure numbers and numeric strings. Prior to v1.0.48, numbers were always converted to strings before being stored in variables. This still happens if you use the slow mode of SetFormat.

Note: Var1 "" and Var1 have the same value and type on AutoHotkey v2 if Var1 contains a string, as it does in this case. Only v1 makes a distinction between quoted strings (or strings concatenated with quoted strings) and other strings. But if both values are strings, v2 does not perform numeric comparison.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 6 guests