无法从数组中返回项目

时间:2017-01-24 20:20:03

标签: arrays autohotkey

我刚开始使用AHK并且正在教自己如何阅读由4列,空格和制表符分隔符以及未知行数组成的文本文件。

我试图获取包含字符串" xcal"的任何行的第一个标记,将第一个标记(%A_Index%= 1,对吗?)存储在我的数组中(RunNum) ,然后检索所有存储的数字,在MsgBox中显示它们。

#SingleInstance, Force

RunNum :=  Object() ; Initialize temporary array

; ----------------- Read LIST.TAB.   ---------------------------

IfNotExist, %A_ScriptDir%\LIST.TAB
{
    MsgBox,48,Error!, LIST.TAB was not found.
        ExitApp
}
else
{
    RunCount = 1 ; Set Run counter 
    Loop, read, %A_ScriptDir%\list.tab ; Read config file
    {
        IfInString, A_LoopReadLine,  xcal ; If current line contains the word 'xcal'...
        {
            Loop, Parse, A_LoopReadLine, %A_Space% %A_Tab% ; Parse through current line of config file, space and tab delimiter 
            {
                if  (%A_Index% = 1) ; Continue if at the first element/token of string
                {   
                 RunNum[RunCount] :=  A_LoopField ; Store current field in RunNum array
                 RunCount+=1  ; Increase counter           
                }
            }               
        }                                         
    }
 MsgBox % RunNum[RunCount]
}

1 个答案:

答案 0 :(得分:1)

根据您的描述使用此数据:

1234    xcal    RandomJunk
4567    Nocal   RandomJunk
8910    xcal    RandomJunk

代码:

#SingleInstance, Force

RunNum := [] ; No reason to use Object()

; ----------------- Read LIST.TAB.   ---------------------------
If !(FileExist(A_ScriptDir "\list.tab")) {
    MsgBox,48,Error!, LIST.TAB was not found.
        ExitApp
} else {
    Loop, read, %A_ScriptDir%\list.tab ; Read config file
    {
        If (InStr(A_LoopReadLine,  "xcal")) ; If current line contains the word 'xcal'...
            RunNum.push(StrSplit(A_LoopReadLine, A_Space A_tab).1)             
    }

For Each, Value in RunNum
    YourNumbers .= Value "`n"
 MsgBox % YourNumbers
}

结果:

1234
8910