1
votes

Trying to read a CSV file in Auto Hot Key and line by line split the line by "," to pull out the last two columns of each line. Currently just trying to get the string to split into an array. I can print each line with the line MsgBox, A_LoopReadLine but cannot split the string inside of the variable.

Have tried StringSplit and StrSplit but I am sure the syntax is incorrect.

MyArray := Object()
Loop, read, %fileop%
{
    MyArray.Insert(A_LoopReadLine) ; Append this line to the array.
    index := 1
    MsgBox, %A_LoopReadLine%
    ;MyArray.
    ;MsgBox, % StrSplit(A_LoopReadLine ,",")
}

Loop % MyArray.Length()
    MsgBox % StrSplit(MyArray[A_Index],",")
1

1 Answers

2
votes

Trying to read a CSV file in Auto Hot Key and line by line split the line by "," to pull out the last two columns of each line.

MyArray := Object()
Loop, Read, %fileop%
    MyArray[A_Index]:=StrSplit(A_LoopReadLine,",")

This will store your csv file in MyArray[row][column] format. E.g to access second item in fifth row: MyArray[5][2]

for k,v in MyArray
    v.RemoveAt(1,v.Length()-2)

Above will remove all but last two items from each row.


Documentation:

https://autohotkey.com/docs/commands/For.htm

https://autohotkey.com/docs/objects/Object.htm#RemoveAt_v1121+

https://autohotkey.com/docs/objects/Object.htm#Length


Edit: And to as to why your code did not work. It kinda did. The thing is that StrSplit() returns object, array so with below line you were trying to display object in MsgBox, this is not allowed.

MsgBox % StrSplit(MyArray[A_Index],",")

This for example would work:

MsgBox % StrSplit(MyArray[A_Index],",")[1]