Assign multiple variables Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
millansoft
Posts: 47
Joined: 19 Aug 2016, 10:10

Assign multiple variables

Post by millansoft » 08 Dec 2022, 16:47

Hello,

I have the following code:

Code: Select all

colors := "red,green,blue"
for index, color in StrSplit(colors, ",")
    MsgBox % "Color number " index " is " color
Instead msgbox, I want to assign a variable for each result that gives for, like this:

Color1 := red
Color2 := green
Color3 := blue

I found it a bit tricky, any ideas?

Thanks

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Assign multiple variables

Post by flyingDman » 08 Dec 2022, 17:01

You can do that like so:

Code: Select all

colors := "red,green,blue"
for index, color in StrSplit(colors, ",")
    Color%index% := color

msgbox % color1
msgbox % color2
msgbox % color3
But think about using a real array rather than a pseudo-array. I.e. :

Code: Select all

colors := "red,green,blue"
color := StrSplit(colors, ",")

msgbox % color.1
msgbox % color.2
msgbox % color.3
14.3 & 1.3.7

millansoft
Posts: 47
Joined: 19 Aug 2016, 10:10

Re: Assign multiple variables

Post by millansoft » 08 Dec 2022, 19:02

Hello,

Thanks for your help, this way works:

Code: Select all

FileRead, DBXML, myoldfile.xml

FileReadLine, clonline, newlines.txt, 2
clon := StrSplit(clonline, ",")
NewDBXML := StrReplace(DBXML, clon[1], clon[2])

FileAppend, %NewDBXML%, finalfile.txt

But when I put it inside a loop to process several lines, just a blank file I get:

Code: Select all

FileRead, DBXML, myoldfile.xml

Loop, 50
{
    FileReadLine, clonline, newlines.txt, %A_Index%
	clon := StrSplit(clonline, ",")
	NewDBXML := StrReplace(DBXML, clon[1], clon[2])
	DBXML := NewDBXML
}

FileAppend, %NewDBXML%, finalfile.txt
What I´m doing wrong? :crazy:

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Assign multiple variables  Topic is solved

Post by flyingDman » 08 Dec 2022, 20:19

We do not know anything about your xml file or your .txt file, but try this:

Code: Select all

FileRead, DBXML, myoldfile.xml

Loop
{
    FileReadLine, clonline, newlines.txt, %A_Index%
	if ErrorLevel
        break
	clon := StrSplit(clonline, ",")
	DBXML := StrReplace(DBXML, clon[1], clon[2])
}

FileRecycle, finalfile.txt
FileAppend, %DBXML%, finalfile.txt
14.3 & 1.3.7

millansoft
Posts: 47
Joined: 19 Aug 2016, 10:10

Re: Assign multiple variables

Post by millansoft » 08 Dec 2022, 20:30

flyingDman wrote:
08 Dec 2022, 20:19
We do not know anything about your xml file or your .txt file, but try this:

Code: Select all

FileRead, DBXML, myoldfile.xml

Loop
{
    FileReadLine, clonline, newlines.txt, %A_Index%
	if ErrorLevel
        break
	clon := StrSplit(clonline, ",")
	DBXML := StrReplace(DBXML, clon[1], clon[2])
}

FileRecycle, finalfile.txt
FileAppend, %DBXML%, finalfile.txt
Thanks a lot for your help, works perfect!

Post Reply

Return to “Ask for Help (v1)”