Loop, Parse and Variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Loop, Parse and Variables

Post by TheDewd » 29 May 2014, 08:34

I have a text file with three strings on each line, separated using a delimter.

Example:
String1*String2*String3
String1*String2*String3
String1*String2*String3
String1*String2*String3
Using Loop, Parse, A_LoopReadLine, *, `r, how can I assign each of the three strings to a variable? Once A_LoopReadLine is on the next line, the variables can be reused.


The code provided below will show each of the three strings in a separate MsgBox. How can I make it read like the following?:
"String 1 is: %String1%, String 2 is: %String2%, and String 3 is: %String3%"

Code: Select all

Loop, Read, SKUS.txt
{
	Loop, Parse, A_LoopReadLine, *, `r
	{
		MsgBox, %A_LoopField%
	}
}

Guest

Re: Loop, Parse and Variables

Post by Guest » 29 May 2014, 08:49

You don't need to loop the A_LoopReadLine variable you can directly use StringSplit

Loop, Read, SKUS.txt
{
StringSplit, string, A_LoopReadLine, *
MsgBox, String 1 is: %String1%, String 2 is: %String2%, and String 3 is: %String3%
}

User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Loop, Parse and Variables

Post by TheDewd » 29 May 2014, 08:55

@Guest,
Thanks! That's perfect.

I had come up with this just now, but I like your method more:

Code: Select all

Loop, Read, SKUS.txt
{
	Loop, Parse, A_LoopReadLine, *, `r
	{
		If A_Index=1
		StyleVar:= A_LoopField
		If A_Index=2
		ColorVar:= A_LoopField
		If A_Index=3
		SizeVar:= A_LoopField
	}
	MsgBox, %StyleVar% %ColorVar% %SizeVar%
}
Return

tyee
Posts: 27
Joined: 07 Oct 2014, 16:54

Re: Loop, Parse and Variables

Post by tyee » 17 Oct 2014, 13:25

I have somewhat a similar problem.

Code: Select all

first := "width=1920"
second := "height=1080"
Commandline := first "," second

Loop, Parse, commandline, `,
	{
StringSplit, string, A_Loopfield, `,
MsgBox, String 1 is: %String1%, String 2 is: %String2%
}
I have vars first and second and want to set variables as so - width := "1920" and height := "1080" but can't quite figure it out.

Guest

Re: Loop, Parse and Variables

Post by Guest » 17 Oct 2014, 13:49

Not exactly sure what you want but perhaps something like this

Code: Select all

first := "width=1920"
second := "height=1080"
Commandline := first "," second

Loop, Parse, commandline, CSV
    {
	 StringSplit, string, A_Loopfield, = ; no point in using , again as that would just give you "first" and "second" which you already have?
	 %String1%:=String2 ; assign value to variable name
	}
MsgBox % "Width = " Width "`nHeight = " Height

tyee
Posts: 27
Joined: 07 Oct 2014, 16:54

Re: Loop, Parse and Variables

Post by tyee » 17 Oct 2014, 14:01

Excellent, thanks a lot! It's so easy when you have experience. I spent a long time on this not getting anywhere even though I'm new to AHK only 3 days.

tyee
Posts: 27
Joined: 07 Oct 2014, 16:54

Re: Loop, Parse and Variables

Post by tyee » 17 Oct 2014, 15:15

Well it's nearly working perfectly, I discovered when I put a filename in as a var it doesn't like certain characters, see below -

Code: Select all

first := "c:\dummyfile.mov"
second := "height=1080"
Commandline := first "," second

Loop, Parse, commandline, CSV
    {
    StringSplit, string, A_Loopfield, = ; no point in using , again as that would just give you "first" and "second" which you already have?
    %String1%:=String2 ; assign value to variable name
   }
MsgBox % "Width = " Width "`nHeight = " Height
Please run it. How to fix??

Update - Fixed by changing -> first := "filename=c:\dummyfile.mkv" ; must have a string1 and a string2 separated by equals.

Post Reply

Return to “Ask for Help (v1)”