Arrays 自动热键:为什么assignment、InsertAt、length和MaxIndex在RegExMatch()生成的数组上不起作用?

Arrays 自动热键:为什么assignment、InsertAt、length和MaxIndex在RegExMatch()生成的数组上不起作用?,arrays,autohotkey,Arrays,Autohotkey,在“自动热键”中,在模式3(匹配对象)中,将匹配的字符串放入。我可以使用数组[index]获取匹配项,也可以使用。但我有以下问题: 当我尝试使用:=更改数组元素的值时,该值不会更改 尝试使用插入元素时,不会插入任何内容 当我尝试使用or获取数组的大小时,什么也得不到 以下脚本演示了此行为: ; "TestScript.ahk" ; Use a regular expression to read data from a string, then test the beh

在“自动热键”中,在模式3(匹配对象)中,将匹配的字符串放入。我可以使用
数组[index]
获取匹配项,也可以使用。但我有以下问题:

  • 当我尝试使用
    :=
    更改数组元素的值时,该值不会更改
  • 尝试使用插入元素时,不会插入任何内容
  • 当我尝试使用or获取数组的大小时,什么也得不到
以下脚本演示了此行为:

; "TestScript.ahk"

; Use a regular expression to read data from a string, then test the behavior of the resulting array.

#NoEnv
#Warn

^+z::                           ; Hotkey for this script: Ctrl-shift-Z
sDataString := "A1B2C3"
nFoundPosn := RegExMatch(sDataString, "O)A(\d)B(\d)C(\d)", asData)
asData[2] := "4"
asData.InsertAt(3, "5")
MsgBox % "I expect the following to produce:`n"
        . "{1, 4, 5, 3}`n"
        . ">4<, >4<, >4<`n"
        . "`n"
        . "but instead I get:`n"
        . "{" . asData[1] . ", " . asData[2] . ", " . asData[3] . ", " . asData[4] . "}`n"
        . ">" . asData.count() . "<, >" . asData.length() . "<, >" . asData.MaxIndex() . "<"
return
;“TestScript.ahk”
; 使用正则表达式从字符串读取数据,然后测试结果数组的行为。
#诺恩
#警告
^+z::;此脚本的热键:Ctrl-shift-Z
sDataString:=“A1B2C3”
nFoundPosn:=RegExMatch(sDataString,“O)A(\d)B(\d)C(\d)”,asData)
asData[2]:=“4”
asData.插页(3,“5”)
MsgBox%“我希望产生以下结果:`n”
. {1,4,5,3}`n

. ">444" . asData.count()。"" . asData.length()。"" . asData.MaxIndex()。“你遇到了一件多么有趣的事情!我以为你肯定犯了错误,但我错了。看来唯一可用的属性就是那些。令人恼火的是,它不能被视为标准对象;你甚至不能克隆它来做有用的工作。作为备受尊敬的AHK论坛用户jeeswg发现(),唯一的选项是循环匹配对象并从中构建数组,如下所示

^+z::
asDataNew := []
sDataString := "A1B2C3"
nFoundPosn := RegExMatch(sDataString, "O)A(\d)B(\d)C(\d)", asData)

Loop , % asData.count()
    asDataNew[A_Index] := asData[A_Index]

asDataNew[2] := "4"
asDataNew.InsertAt(3, "5")
MsgBox % "I expect the following to produce:`n"
        . "{1, 4, 5, 3}`n"
        . ">4<, >4<, >4<`n"
        . "`n"
        . "but instead I get:`n"
        . "{" . asData.1 . ", " . asData[2] . ", " . asData[3] . ", " . asData[4] . "}`n"
        . ">" . asData.count() . "<, >" . asData.length() . "<, >" . asData.MaxIndex() . "<`n"
        . "`n"
        . "With hard copy I get:`n"
        . "{" . asDataNew.1 . ", " . asDataNew[2] . ", " . asDataNew[3] . ", " . asDataNew[4] . "}`n"
        . ">" . asDataNew.count() . "<, >" . asDataNew.length() . "<, >" . asDataNew.MaxIndex() . "<"
return
^+z::
asDataNew:=[]
sDataString:=“A1B2C3”
nFoundPosn:=RegExMatch(sDataString,“O)A(\d)B(\d)C(\d)”,asData)
循环%asData.count()
asDataNew[A_Index]:=asData[A_Index]
asDataNew[2]:=“4”
asDataNew.InsertAt(3,“5”)
MsgBox%“我希望产生以下结果:`n”
“{1,4,5,3}`n”

.“>444.asData.count().asData.length().asData.MaxIndex().asDataNew.count().asDataNew.length().asDataNew.MaxIndex()。jeeswg的那篇文章也提出了这样一个原因:“优化……考虑到大字符串,其中只读是有利的。”这是有道理的,因为正则表达式确实经常(包括我)在非常大的字符串上使用,这让我感觉更好,因为我不得不写一个小循环来将结果转换成我可以操纵的东西。如您所示,循环的代码很简单。@NewSites啊,我明白了,谢谢您的洞察力。尽管如此,至少能够克隆它还是不错的。