Autohotkey 从文件中查找数据并将其存储为变量

Autohotkey 从文件中查找数据并将其存储为变量,autohotkey,Autohotkey,我是自动热键新手,我不知道如何解决这个问题。感谢您的帮助。 我有list.txt,其中包括如下ID和名称: list.txt: 123124 - whatever 834019 - sometext 3980 - afjalkfj 我需要一个函数,可以做到以下几点 lookup(id, name){ ** The function here should lookup for the id inserted then save ONLY the data related to

我是自动热键新手,我不知道如何解决这个问题。感谢您的帮助。 我有list.txt,其中包括如下ID和名称:

list.txt:
123124 - whatever
834019 - sometext
3980   - afjalkfj
我需要一个函数,可以做到以下几点

lookup(id, name){
    ** The function here should lookup for the id inserted 
 then save ONLY the data related to it in variable x (not the full line)
}
范例

lookup(834019, x)
%x% = sometext

请帮我做这个。谢谢

在这种情况下,您需要的是

  • 将文件内容读入变量

  • A来解析每行的文本

  • 将每行的文本拆分为 使用指定分隔符的子字符串数组

在这种情况下,第二个参数(名称)是多余的。您可以省略它:

x := lookup(834019)
MsgBox, % x

MsgBox, % lookup(3980)


lookup(id) {
    FileRead, Contents, list.txt   ; read the file's contents into the variable "Contents"
    if not ErrorLevel  ; Successfully loaded.
    {
        Loop, parse, Contents, `n, `r  ; parse the text of each line 
        {
            word_array1 := StrSplit(A_LoopField," - ").1  ; store the first substring into the variable "word_array1"
            word_array1 := Trim(word_array1, " `t") ; trim spaces and tabs in this variable
            If (word_array1 = id)
            {
                name := StrSplit(A_LoopField," - ").2
                name := Trim(name, " `t")
                return name
            }
        }
        Contents := ""  ; Free the memory.
    }
    else
        MsgBox, A problem has been encountered while loading the File Contents
}

非常感谢你!这对我帮助很大。我现在唯一的问题是,我希望信息存储在第二个参数(名称)中,例如,如果我运行查找(10000004,name1)并在脚本中稍后使用%name1%,它仍然会显示名称