AHK 从文本文件中 Grepping 一个数组
AHK Grepping an Array from Text File
我编写了一个 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
#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
好的,所以我没有完全实现您的设计。看起来您的剪贴板分配使用的是已弃用的 = 符号 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
}
我编写了一个 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
#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
好的,所以我没有完全实现您的设计。看起来您的剪贴板分配使用的是已弃用的 = 符号 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
}