Trouble splitting comma separated data into array Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
omar
Posts: 462
Joined: 22 Oct 2015, 17:56

Trouble splitting comma separated data into array

22 Jan 2021, 16:58

I'm reading in from clipboard some data like this:

abc,123
efg,456
xyz,999

I want to read each line and then get the 2 items read into a variable

I'm trying this:

Code: Select all

Loop, parse, clipboard, `n, `r
{
StringSplit, myArray, A_LoopField, `,
MsgBox % myArray[1] + myArray[1]
}
Not working.
What am I doing wrong?
Thanks.
User avatar
mikeyww
Posts: 5300
Joined: 09 Sep 2014, 18:38

Re: Trouble splitting comma separated data into array

22 Jan 2021, 17:03

AHK uses + to indicate mathematical addition in expressions.

Code: Select all

Clipboard =
(
abc,123
efg,456
xyz,999
)
Loop, Parse, Clipboard, `n, `r
{
 part := StrSplit(A_LoopField, ",")
 MsgBox % part[1] "`n" part[2]
}
:arrow: Concatenation
omar
Posts: 462
Joined: 22 Oct 2015, 17:56

Re: Trouble splitting comma separated data into array

22 Jan 2021, 18:23

@mikeyww I have a small problem

My data is:

abc,123
efg,456
xyz,999

Now I copy this into clipboard.
But the final line has a new line character.
So the loop gets executed one extra time.

I've tried trimming the clipboard by using trim(Clipboard)
Doesn't seem to work.

Any ideas?

Thanks.
User avatar
mikeyww
Posts: 5300
Joined: 09 Sep 2014, 18:38

Re: Trouble splitting comma separated data into array  Topic is solved

22 Jan 2021, 20:18

Code: Select all

n := 0
Clipboard =
(
abc,123
efg,456
xyz,999



)
Loop, Parse, % Trim(Clipboard, "`n"), `n, `r
 MsgBox,, % "Result #" ++n, % (part := StrSplit(A_LoopField, ",")).1 "`n" part.2
omar
Posts: 462
Joined: 22 Oct 2015, 17:56

Re: Trouble splitting comma separated data into array

28 Jan 2021, 18:52

@mikeyww That's awesome
I get stuck on this SAME problem again and again and forget the answer
This time I have made a bookmark! 🙂

Return to “Ask For Help”

Who is online

Users browsing this forum: bonswouar, hasantr, MrCheeseman, TAC109, theon, vmech, XMCQCX and 37 guests