用大数字作为数组的键名的坑

供新手入门和老手参考的教程和相关资料,包括中文帮助

Moderators: tmplinshi, arcticir

Post Reply
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

用大数字作为数组的键名的坑

Post by tmplinshi » 14 Nov 2017, 00:31

Code: Select all

n := "448045523405"
obj := {}
obj[n] := "abc"

MsgBox, % obj[n] ; 结果为 abc,正常。

for k, v in obj
	MsgBox, % k "=" v ; 但是这里显示的是 1368924621=abc

Code: Select all

n1 := "448045523405"
n2 := "1368924621"

obj := {}
obj[n1] := "abc"
obj[n2] := "efg"

MsgBox, % obj[n1] ; 结果变成了 efg

; 只有一个键值:1368924621=efg
for k, v in obj
	MsgBox, % k "=" v
解决方法:键名前面加上非数字,或者键名两边加引号

Code: Select all

n1 := "448045523405"
n2 := "1368924621"

obj := {}
obj["" n1 ""] := "abc"
obj["_" n2] := "efg"

MsgBox, % obj[n1] ; 结果为空
MsgBox, % obj["" n1 ""] ; 结果为abc
MsgBox, % obj["_" n2] ; 结果为efg

; 448045523405=abc
; _1368924621=efg
for k, v in obj
	MsgBox, % k "=" v

xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: 用大数字作为数组的键名的坑

Post by xuezhe » 14 Nov 2017, 03:46

这是漏洞吗。最好报告给ahk作者呀。这是L V1 的漏洞,估计作者也不升级了。

我用V2运行结果正常,没有上面的问题。

Code: Select all

n := "448045523405"
obj := {}
obj[n] := "abc"

MsgBox  obj[n] ; 结果为 abc,正常。

for k, v in obj
	MsgBox  k "=" v  ; 448045523405=abc

n1 := "448045523405"
n2 := "1368924621"

obj := {}
obj[n1] := "abc"
obj[n2] := "efg"

MsgBox obj[n1] ; abc

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: 用大数字作为数组的键名的坑

Post by jeeswg » 14 Nov 2017, 07:45

Objects
https://autohotkey.com/docs/Objects.htm
•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.
Objects
https://lexikos.github.io/v2/docs/Objects.htm
•Integer keys are stored using the native signed integer type where possible. Integers which are less than -2147483648 or greater than 2147483647 are stored as strings on AutoHotkey 32-bit but as integers on AutoHotkey 64-bit. (By contrast, 64-bit integers can be stored as values on either version.)
Cheers. 干杯。
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 用大数字作为数组的键名的坑

Post by tmplinshi » 14 Nov 2017, 10:18

Oh, thank you jeeswg! This is exactly the document I was looking for.

@xuezhe 我猜测会有相应的文档说明,jeeswg帮忙找出来了 :)

fywlts
Posts: 3
Joined: 29 Aug 2018, 00:07

Re: 用大数字作为数组的键名的坑

Post by fywlts » 22 Apr 2020, 03:37

键应该是一个变量吧。用变量就没事。如果是数字,不连续的话,不知道如何,但多维的话,可以用array[y,x]:=的形式赋值

Post Reply

Return to “教程资料”