Bug! Array key variable not working when is string with number

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Bug! Array key variable not working when is string with number

03 Nov 2019, 09:47

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!
Last edited by ShadowMaster85 on 03 Nov 2019, 10:11, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Bug! Array key variable not working when is string with number

03 Nov 2019, 10:01

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.
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Re: Bug! Array key variable not working when is string with number

03 Nov 2019, 10:20

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
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Bug! Array key variable not working when is string with number

03 Nov 2019, 11:44

That is a separate issue, which I'm pretty certain is intended, the behaviour is consistent with :arrow: if var is integer (and float and number),
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.
It could be mentioned in other more obvious places though.

Cheers.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Bug! Array key variable not working when is string with number

04 Nov 2019, 03:37

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.)

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
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] :lol:
iPhilip
Posts: 820
Joined: 02 Oct 2013, 12:21

Re: Bug! Array key variable not working when is string with number

04 Nov 2019, 12:54

lexikos wrote:
04 Nov 2019, 03:37
This also works if you're using Unicode (and saving as UTF-8 with BOM): a["🔑" k] :lol:
That's funny! :lol:
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Re: Bug! Array key variable not working when is string with number

04 Nov 2019, 13:16

lexikos wrote:
04 Nov 2019, 03:37
This also works if you're using Unicode (and saving as UTF-8 with BOM): a["🔑" k] :lol:
Yes that's funny! :lol: :lol: :lol:

Thanks for clarification!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Bug! Array key variable not working when is string with number

04 Nov 2019, 14:16

                      :lol:  :lol:  :lol:  :lol:
                      :lol:  :lol:        :lol:
                      :lol:  :lol:  :lol:  :lol:
                      :lol:  :lol:  :lol:  :lol:
                   :lol:
               :lol:
          :lol: :lol:
     :lol:
:lol: :lol:
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Google [Bot], Joey5, RandomBoy, Rohwedder and 380 guests