How can I split tab-delimited strings in Autohotkey?

孤街浪徒 提交于 2020-01-05 04:24:09

问题


I have a series of tab-delimited strings copied to the Windows Clipboard. I want to split these strings into arrays using the tab character.

Unit    Dept_ID Name
CORP    0368    Admin
CORP    3945    Programmer
SESHAN  4596    Software Engineer   

I'm trying to use StringSplit(), but I can't figure out how to use 'tab' as my delimiter. I've tried a few different methods, but none seem to work.

clipboard = %clipboard%  ; Convert to plain text
StringSplit, MyArray, clipboard, `\t` ; Split string using tabs
MsgBox % "MyArray[1] = " . MyArray[1] ; BUG: Prints empty string

How can I split a tab-delimited string in AutoHotkey?


回答1:


First you need to split them to an array of lines with:

lines := StrSplit(clipboard, "`n")

Then you can loop over all the lines and split them to columns creating a multidimensional array:

columns := []
for index, value in lines
    columns.Insert(StrSplit(value, "`t"))
; examples
MsgBox % columns[1][2] ; Dept_ID
MsgBox % columns[2][1] ; CORP
MsgBox % columns[2][2] ; 0368

Note that Autohotkey has 2 types of arrays the "new" type which are actually objects and you use them with arr[index] and the older pseudo arrays. In your code you mixed them up, StringSplit returns a pseudo array and can't be used with []. I recommend you read about arrays in the documentation.




回答2:


This splits tab delimited clipboard contents into an array

MyArray := StrSplit( clipboard, "`t" )
MsgBox % "MyArray[1] = " . MyArray[1]

Functionally equivalent

StringSplit MyArray, clipboard, `t
MsgBox MyArray1 = %MyArray1%


来源:https://stackoverflow.com/questions/45620437/how-can-i-split-tab-delimited-strings-in-autohotkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!