Variable name from array variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JemyM
Posts: 44
Joined: 04 Jul 2017, 11:57

Variable name from array variable

21 Aug 2018, 09:44

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?
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Variable name from array variable

21 Aug 2018, 10:25

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
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Variable name from array variable

21 Aug 2018, 14:29

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"]
my scripts
jernijka
Posts: 3
Joined: 16 Aug 2018, 13:39

Re: Variable name from array variable

21 Aug 2018, 14:41

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"]
Strange because after throwing this array into the iteration loop, it makes me fall into error.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Variable name from array variable

21 Aug 2018, 20:20

Hi jernijka and welcome to the AutoHotkey forum,
jernijka wrote:Strange because after throwing this array into the iteration loop, it makes me fall into error.
- What kind of error is thrown? Could you please provide a sample code demonstrating the behaviour?
- 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
my scripts
jernijka
Posts: 3
Joined: 16 Aug 2018, 13:39

Re: Variable name from array variable

22 Aug 2018, 11:30

Last edited by jernijka on 31 Oct 2020, 15:41, edited 6 times in total.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Variable name from array variable

22 Aug 2018, 19:08

jernijka wrote:My bad now it work! :)
Cool! :thumbup:
my scripts
JemyM
Posts: 44
Joined: 04 Jul 2017, 11:57

Re: Variable name from array variable

23 Aug 2018, 12:29

for key, value in array

this was what I was looking for. Thank you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, mikeyww and 321 guests