Array key unexpectedly converted to int Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Array key unexpectedly converted to int

Post by pneumatic » 05 May 2021, 22:27

For some reason AHK appears to be interpreting one of my array keys as an integer despite being a string:

Code: Select all

keys := ["0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01"
	  ,"01234567890123456789012345678901234567890123456789"]

a := []
a.push( {keys[1] : "value1"} )
a.push( {keys[2] : "value2"} )

loop % a.length()
	for key , val in a[A_Index]
		str .= key . "`n"

msgbox % str
; output:
; 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01  (good)
; 9223372036854775807 (bad)
9223372036854775807 is the maximum integer size for 64-bit, so not only is it the wrong value but data is probably being lost.
Last edited by pneumatic on 05 May 2021, 22:36, edited 1 time in total.
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Re: Array key unexpectedly converted to int

Post by pneumatic » 05 May 2021, 22:33

Oh it seems appending "" to the keys during the push() fixes it.

(but Format("{:s}") doesn't)

Nevermind.
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: Array key unexpectedly converted to int  Topic is solved

Post by lexikos » 06 May 2021, 03:03

This and many other quirks are fixed by v2.
Integer keys are stored using the native signed integer type. AutoHotkey 32-bit supports integer keys in the range -2147483648 to 2147483647. AutoHotkey supports 64-bit integers, but only AutoHotkey 64-bit supports the full range as keys in an object.
Source: Objects - Definition & Usage | AutoHotkey
"Integer keys" was probably supposed to include numeric strings, as v1 is not designed to consistently allow for distinction between a numeric string and a pure integer. For instance, x := "1", y := x + 1 produces an x which has both a pure integer and a string, and so does x := 1 or x := 01 (the latter two producing different strings in v1). This is due to caching (but I'm just using it as an example; caching isn't the cause of your issue).

I will clarify the documentation for "Keys".

Edit: It's actually implied by the next point:
As a consequence of the point above, the string format of integer values is not retained. For example, x[0x10], x[16] and x[00016] are equivalent. This also applies to numeric strings which don't have a decimal point.
Post Reply

Return to “Ask for Help (v1)”