AutoHotkey:为什么赋值、InsertAt、长度和 MaxIndex 对 RegExMatch() 生成的数组不起作用?
AutoHotkey: Why are assignment, InsertAt, length, and MaxIndex, not working on an array generated by RegExMatch()?
在 AutoHotkey 中,RegExMatch() in mode 3 (match object) puts the matched strings in an array. I can get the matches using array[index]
, and I can get the number of matches using .count()。但是我有以下问题:
- 当我尝试使用
:=
更改数组元素的值时,该值没有改变。
- 当我尝试使用 .InsertAt 插入元素时,没有插入任何内容。
- 当我尝试使用 .length() or .MaxIndex() 获取数组的大小时,我什么也没得到。
此行为由以下脚本演示:
; "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
产生以下输出:
显然,对于正在生成的数组以及如何使用它,我有一些不了解的地方。赋值和插入不起作用的事实向我表明数组变量是一个指针,但我在上面链接的文档中没有看到任何关于它的信息,所以如果它是一个指针,我不知道如何访问它指向的对象。我做错了什么,或者我误解了什么?
你遇到了什么有趣的事情!我以为你肯定犯了错误,但我错了。似乎唯一可用的属性是那些 listed in the help documentation for that mode. Annoyingly, it cannot be treated as a standard object; you can't even clone it to do useful work with it. As highly-esteemed AHK forum user jeeswg found (here),唯一的选择是循环遍历匹配对象并从中构建一个数组,如下所示。
^+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
在 AutoHotkey 中,RegExMatch() in mode 3 (match object) puts the matched strings in an array. I can get the matches using array[index]
, and I can get the number of matches using .count()。但是我有以下问题:
- 当我尝试使用
:=
更改数组元素的值时,该值没有改变。 - 当我尝试使用 .InsertAt 插入元素时,没有插入任何内容。
- 当我尝试使用 .length() or .MaxIndex() 获取数组的大小时,我什么也没得到。
此行为由以下脚本演示:
; "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
产生以下输出:
显然,对于正在生成的数组以及如何使用它,我有一些不了解的地方。赋值和插入不起作用的事实向我表明数组变量是一个指针,但我在上面链接的文档中没有看到任何关于它的信息,所以如果它是一个指针,我不知道如何访问它指向的对象。我做错了什么,或者我误解了什么?
你遇到了什么有趣的事情!我以为你肯定犯了错误,但我错了。似乎唯一可用的属性是那些 listed in the help documentation for that mode. Annoyingly, it cannot be treated as a standard object; you can't even clone it to do useful work with it. As highly-esteemed AHK forum user jeeswg found (here),唯一的选择是循环遍历匹配对象并从中构建一个数组,如下所示。
^+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