AHK从文本文件中获取数组

时间:2017-04-07 06:34:56

标签: autohotkey

我写了一个AHK脚本,通过InputBox提示用户输入,逐行导航基于用户输入的文本文件,并将输出复制到剪贴板。

示例:

  • 如果" 4max"作为UserInput给出,它应该复制输出" sleep"至 剪贴板。

  • 如果" 3ben"作为UserInput给出,它应该复制输出" jog"至 剪贴板。

示例文字文件:

max:eat:drink:sleep:play
jerry:eat:play:drink:jog
laura:drink:eat:sleep:play
ben:sleep:jog:eat:drink

有人可以加强以下脚本吗?

此外,当我打开空白记事本的脚本时,它会将剪贴板中的当前项目粘贴到记事本中。 :(

脚本:

#SingleInstance, force
#Include C:\Users\mpechett\Desktop\ahk\tf.ahk

InputBox, SearchText, Search for Name

x = %SearchText%
RegExMatch(x, "(\d*)(\w*)", y)
SearchText:= % y2
ptext = % TF_Find("C:\Users\mpechett\Desktop\ahk\test.txt", "","", SearchText, 1, 1)
StringSplit, word_array, ptext, :, .

;msgBox % word_array%y1%

Clipboard = % word_array%y1%
Msgbox, %Clipboard%

Esc::ExitApp

2 个答案:

答案 0 :(得分:0)

#SingleInstance, force
SetTitleMatchMode, 2
SetWorkingDir, %A_ScriptDir% ; if an absolute path of the file isn't specified

; Put tf.ahk in the folder:  %A_MyDocuments%\AutoHotkey\Lib\TF-master
#Include <TF-master\tf>

F1:: ; assign a hotkey to the code (the script is persistent because of "Esc::ExitApp").
InputBox, SearchText, Search for Name
if ErrorLevel
    return
x = %SearchText%
RegExMatch(x, "(\d*)(\w*)", y)
SearchText:= % y2
ptext = % TF_Find("C:\Users\mpechett\Desktop\ahk\test.txt", "","", SearchText, 1, 1)
StringSplit, word_array, ptext, :, .

; Instead of the clipboard you can use another variable to save the output:
myVar = % word_array%y1%
; Msgbox, %myVar%

IfWinExist, Untitled - Notepad
{
    WinActivate 
    WinWaitActive
    SendInput, %myVar%
}
return

Esc::ExitApp

答案 1 :(得分:0)

好的,所以我没有完全实现你的设计。看起来你的剪贴板作业使用的是deprecated = sign vs:=现在是分配对象的标准,这可能是一个问题。我没有测试你的代码,决定重写整个解决方案更好,而不使用任何外部库。

尝试:

    testdata =
    (
    max:eat:drink:sleep:play
    jerry:eat:play:drink:jog
    laura:drink:eat:sleep:play
    ben:sleep:jog:eat:drink
    )

    InputBox, SearchText, Search for Name

    MsgBox % clipboard := grepFile(SearchText, testdata)

    grepFile(userinput, file) {
        For e, v in StrSplit(userinput) {
            If v is alpha
                key .= v
            else 
                num .= v
        }

        arr := formatFile(file)

        return arr[key][num]
    }

    formatFile(file) {
        ourObj := {}
        For e, line in StrSplit(file, "`n", "`r") {
            arr := StrSplit(line, ":")
            ourObj[(arr.1)] := arr             
        }
        return ourObj
    } 

编辑:

testdata =
(
max-test:eat:drink:sleep:play
jerry-test:eat:play:drink:jog
laura-test:drink:eat:sleep:play
ben-test:sleep:jog:eat:drink
)

testData := StrReplace(testData, "-test")

InputBox, SearchText, Search for Name

MsgBox % clipboard := grepFile(SearchText, testdata)

grepFile(userinput, file) {
    For e, v in StrSplit(userinput) {
        If v is alpha
            key .= v
        else 
            num .= v
    }

    arr := formatFile(file)

    return arr[key][num]
}

formatFile(file) {
    ourObj := {}
    For e, line in StrSplit(file, "`n", "`r") {
        arr := StrSplit(line, ":")
        ourObj[(arr.1)] := arr             
    }
    return ourObj
}