Separate each line of variable into individual variable

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Separate each line of variable into individual variable

Post by Dominic_Fichera » Mon Sep 08, 2014 10:58 am

Hey guys,

Currently I have some code which will take the data off the clipboard and assign it to a variable. I want to know how to separate the lines of this single variable into multiple other variables.

Line 1: This
Line 2: is
Line 3: my
Line 4: macro

for example, with the above on my clipboard,

[snippet=]GetClipboard>varClipboard
StringReplace>varClipboard,Line 1: ,,varClipboard
StringReplace>varClipboard,Line 2: ,,varClipboard
StringReplace>varClipboard,Line 3: ,,varClipboard
StringReplace>varClipboard,Line 4: ,,varClipboard[/snippet]

Then the content of the variable would be:

This
is
my
macro

How would I assign each word to their own variable? Each line could potentially be a different length each time, so MidStr wouldn't be an option, I don't think, unless there was a way for "Length" to determine the length of each line in the variable.

Thank you in advance,

Dominic Fichera

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Separate each line of variable into individual variable

Post by hagchr » Mon Sep 08, 2014 8:06 pm

Hi,

If I understand your problem correctly then you could use RegEx to pull out the information in each line and assign it to variables:

Code: Select all

GetClipboard>varClipboard
Let>tmp0=(?m)(?<=^Line \d: ).+?$
RegEx>tmp0,varClipboard,0,M,NM,0
In case you have not used RegEx before, what it says is, look for a pattern (here given by tmp0) in the string varClipboard and put the total number of hits into the variable NM and put each hit into the array M (M_1, M_2, ...).

The pattern says go line-by-line (?m), look for a position after "^Line \d: " and then take all characters until the end of the line. "^" means beginning of line, "\d" means digit, "." means any character, "+" means 1 or more, and "$" means end of line.

So the result will be: NM=4 (4 hits), M_1=This, M_2=is, etc.

The search pattern needs to be modified if your clipboard string looks different than in your example.

Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Re: Separate each line of variable into individual variable

Post by Dominic_Fichera » Mon Sep 08, 2014 9:04 pm

Thanks for your reply hagchr :)

I've been able to get my script working like this. Thanks for explaining how RegEx works.

Regards,

Dominic Fichera

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts