在 for 循环中访问数组内容 - Autohotkey AHK

accessing array content whithin for loop - Auto Hot Key AHK

我正在尝试将索引 i 处的数组成员的内容提取到一个变量中,然后将其附加到一个文件中。 我怎么做 ? 这是我尝试过的但它不会采用 cgi[i]

的内容
firstrun(){
GuiControlGet, cgiDelay,,_cgiDelay
returnCode:=[]
for i in cgi {
    msg := "http://" ip "/Nexus.cgi?session=" session "&action=" firstRunCgi[i] "&tokenoverride=1"
    sendToHttp(msg)
    getRespond()
    returnCode[i]:=parseReturnCode()
    if (returnCode[i] !=0){
        addTextToGui("Setting 1st run Fail #: " i "`terrorCode: " returnCode[i] "`t"firstRunCgi[i])
        txt = `ncgi[i],skipped
        FileAppend, %txt%, cgiLog.txt
        cgi[i] :=""
        }
    else{
    ;   addTextToGui("Setting 1st run OK #: " i "`terrorCode: " returnCode[i] "`t"firstRunCgi[i])
    }
    Sleep (cgiDelay)
}

}

现在知道为什么以及如何解决了这个问题:

            txt := "`n" cgi[i] ",skipped"
        FileAppend, %txt%, cgiLog.csv

看这里:AutoHotKey: How to access array with counter variable

在您发布的示例中:For i in cgi 变量 i 包含您的 Key/Index,因此您可以使用 Key/Index 通过以下方式访问 cgi 数组中的值:value := cgi[i]

或者你可以声明两个变量来保存你的 Key/Index 和 Value 直接在你的 For 循环中,像这样:

cgi := ["Hello", "World"]
For i, v in cgi ; Notice the comma
     MsgBox % "This is the Key/Index: " i 
            . "`nThis is the Value from the For loop: " v
            . "`nThis Value was accessed using Key/Index: " cgi[i]