So lets say I have textfiles like person1.txt to person9.txt, where each text contains something like;
Name = Alicia
City = London
Occupation = Doctor
I want the text right of the "=" (Alicia) to be stored in a variable called the same as whatever is to the left of the "=" (Name).
So my idea was to just loop through the files, do a StrSplit of each line, and tried the following line;
Person := StrSplit("Name = Alicia"," = ")
%Person[1]% := Person[2]
MsgBox, % Name
But this doesn't work.
Error: This parameter contains a variable name missing its ending percent sign.
I have to do something like;
variablelabel := person[1]
%variablelabel% := person[2]
Is there any way to work around this without adding another varible?
Variable name from array variable
Re: Variable name from array variable
Code: Select all
#SingleInstance, Force
;FileRead, Person1, Person1.txt
Person1 =
(
Name = Alicia
City = London
Occupation = Doctor
)
Loop, Parse, Person1, `n
{
RegExMatch(A_LoopField, "(.*)\s=\s(.*)", Match)
%Match1% := Match2
}
MsgBox, % Name "`n" City "`n" Occupation
Re: Variable name from array variable
Alternatively, you might find handiest to use associative arrays:
Code: Select all
#NoEnv
#Singleinstance force
#Warn
Person1 =
(LTrim Join`n
Name = Alicia
City = London
Occupation = Doctor
)
pairs := StrSplit(Person1, [ A_Space "=" A_Space, "`n"]) ; delimiters can be either a single string or an array of strings, each of which is in this case used to determine where the boundaries between substrings occur
myObject := {}
for key, value in Object(pairs*) ; https://autohotkey.com/docs/Functions.htm#VariadicCall
myObject[key] := value
MsgBox % myObject["Name"]
MsgBox % myObject["City"]
MsgBox % myObject["Occupation"]
Re: Variable name from array variable
Strange because after throwing this array into the iteration loop, it makes me fall into error.A_AhkUser wrote:Alternatively, you might find handiest to use associative arrays:
Code: Select all
#NoEnv #Singleinstance force #Warn Person1 = (LTrim Join`n Name = Alicia City = London Occupation = Doctor ) pairs := StrSplit(Person1, [ A_Space "=" A_Space, "`n"]) ; delimiters can be either a single string or an array of strings, each of which is in this case used to determine where the boundaries between substrings occur myObject := {} for key, value in Object(pairs*) ; https://autohotkey.com/docs/Functions.htm#VariadicCall myObject[key] := value MsgBox % myObject["Name"] MsgBox % myObject["City"] MsgBox % myObject["Occupation"]
Re: Variable name from array variable
Hi jernijka and welcome to the AutoHotkey forum,
- It is possible that what's at stake is the nature of your input string; in particular, the sample code above assumes that equal signs are surronded by space characters unlike the following one:
- What kind of error is thrown? Could you please provide a sample code demonstrating the behaviour?jernijka wrote:Strange because after throwing this array into the iteration loop, it makes me fall into error.
- It is possible that what's at stake is the nature of your input string; in particular, the sample code above assumes that equal signs are surronded by space characters unlike the following one:
Code: Select all
#NoEnv
#Singleinstance force
#Warn
Person1 =
(LTrim Join`n
Name=Alicia
City=London
Occupation=Doctor
)
pairs := StrSplit(Person1, ["=", "`n"]) ; delimiters can be either a single string or an array of strings, each of which is in this case used to determine where the boundaries between substrings occur
myObject := Object(pairs*) ; https://autohotkey.com/docs/Functions.htm#VariadicCall
for key, value in myObject
MsgBox % key "," value
Re: Variable name from array variable
My bad now it work!
--------------------------------------------------------------------
stylufka.pl/wlosy-do-ramion-ombre-niezmiennie-trendy
xn--adwokatkocielny-n4c.pl/stwierdzenie-niewaznosci-malzenstwa
lexform.pl/obsluga-prawna-firm
mobi-prawnik.pl
--------------------------------------------------------------------
stylufka.pl/wlosy-do-ramion-ombre-niezmiennie-trendy
xn--adwokatkocielny-n4c.pl/stwierdzenie-niewaznosci-malzenstwa
lexform.pl/obsluga-prawna-firm
mobi-prawnik.pl
Last edited by jernijka on 31 Oct 2020, 15:41, edited 6 times in total.
Re: Variable name from array variable
for key, value in array
this was what I was looking for. Thank you.
this was what I was looking for. Thank you.