When using that code:
a := []
a[0] := 22
a["0"] := 27
k := "0"
MsgBox % a[k]
The result of MsgBox % a[k] should return 27 but instead return 22, like the value of the key k is a number 0 but instead is a string "0" wich is different, infact when access directly a[0] and a["0"] the returned values are 22 and 27 respectively wich is correct.
Also when the key of the array is "0 "
a := []
a[0] := 22
a["0 "] := 30
k := "0 "
MsgBox % a[k]
The result of MsgBox % a[k] is always 22, so the value of k "0 " is interpreted as a numeric 0
I am using AutoHotkey v1.1.31.01 64 bit on Windows 10 64 bit.
Thanks for Help!
Bug! Array key variable not working when is string with number
-
- Posts: 10
- Joined: 21 Oct 2019, 13:26
Bug! Array key variable not working when is string with number
Last edited by ShadowMaster85 on 03 Nov 2019, 10:11, edited 1 time in total.
Re: Bug! Array key variable not working when is string with number
This is documented behaviour,
objects wrote: Quoted literal strings are considered purely non-numeric in v1.x, so x[1] and x["1"] are not equivalent. Additionally, if a quoted literal string is concatenated with another value (as in "0x" x), the result is treated as purely non-numeric. However, this does not apply to variables, so x[1] and x[y:="1"] are equivalent. This issue will be resolved in AutoHotkey v2, so scripts should avoid using quoted numeric literals as keys.
-
- Posts: 10
- Joined: 21 Oct 2019, 13:26
Re: Bug! Array key variable not working when is string with number
Ok, but this also happens when the key of the array is "0 ", where over the number 0 there is a space, like in this code:
a := []
a[0] := 22
a["0 "] := 30
k := "0 "
MsgBox % a[k]
The result of MsgBox % a[k] is always 22, so the value of k "0 " is interpreted as a numeric 0
a := []
a[0] := 22
a["0 "] := 30
k := "0 "
MsgBox % a[k]
The result of MsgBox % a[k] is always 22, so the value of k "0 " is interpreted as a numeric 0
Re: Bug! Array key variable not working when is string with number
That is a separate issue, which I'm pretty certain is intended, the behaviour is consistent with if var is integer (and float and number),
Cheers.
It could be mentioned in other more obvious places though.is wrote: True if Var is non-empty and contains a purely numeric string (decimal or hexadecimal) without a decimal point. Leading and trailing spaces and tabs are allowed. The string may start with a plus or minus sign.
Cheers.
Re: Bug! Array key variable not working when is string with number
The behaviour should be consistent with every part of the language that converts strings to numbers or checks if strings are numeric. (But for quoted literal strings used directly, v1 does not check if the string is numeric because it already "knows" that numbers should not be enclosed in quote marks.)
If your intent is for every string to be unique, leading and trailing whitespace isn't the only problem. These are all equal, with or without leading or trailing whitespace: "10", "0xA", "010", "0x0A", "+10", "+010", "+0x0A" and so on.
There's another problem: if the key appears to be an integer string but is outside the range of valid integer keys (which varies between 32-bit and 64-bit versions), it will be equivalent to 0, -1, 2**63-1 (max int64) or -2**63 (min int64).
All of this can be avoided by simply making the key non-numeric: a["#" k].
Or less simply, by utilizing COM objects or migrating to AutoHotkey v2 (alpha).
This also works if you're using Unicode (and saving as UTF-8 with BOM): a["" k]
Code: Select all
a := " 0 "
b := 0
MsgBox % a == b ; a and b are equal
a := " 21 "
MsgBox % a * 2 ; a is a valid number
There's another problem: if the key appears to be an integer string but is outside the range of valid integer keys (which varies between 32-bit and 64-bit versions), it will be equivalent to 0, -1, 2**63-1 (max int64) or -2**63 (min int64).
All of this can be avoided by simply making the key non-numeric: a["#" k].
Or less simply, by utilizing COM objects or migrating to AutoHotkey v2 (alpha).
This also works if you're using Unicode (and saving as UTF-8 with BOM): a["" k]
Re: Bug! Array key variable not working when is string with number
That's funny!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
-
- Posts: 10
- Joined: 21 Oct 2019, 13:26
Re: Bug! Array key variable not working when is string with number
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA