Page 1 of 1
Variable as variable name
Posted: 06 Dec 2019, 12:00
by Martin
I have the following code:
Code: Select all
QA := 000
WA := 123
GB := 222
LIST := "QA,WA,GB"
split := StrSplit(LIST, ",")
Loop % split.MaxIndex()
{
;res = %split[A_index]%
;res = % split[A_index]
;res = split[A_index]
;res := %split[A_index]%
;res := % split[A_index]
res := split[A_index]
Msgbox % res
}
I want to get 000, 123, 22 but I always receive QA,WA,GB.
How I do this?
Re: Variable as variable name
Posted: 06 Dec 2019, 12:03
by teadrinker
Re: Variable as variable name
Posted: 06 Dec 2019, 13:52
by Martin
Unfortunately, it doesn't work
Its the same..
Re: Variable as variable name
Posted: 06 Dec 2019, 14:03
by teadrinker
I don't know, how you tried. For me it works like this:
Code: Select all
QA := 000
WA := 123
GB := 222
LIST := "QA,WA,GB"
split := StrSplit(LIST, ",")
Loop % split.MaxIndex() {
res := split[A_index]
Msgbox % %res% ; 000, 123, 222
}
Re: Variable as variable name
Posted: 06 Dec 2019, 14:14
by Martin
I'm sorry, it actually works, but not as I expected...
Let's say that "Loop" is inside a function and instead of "Msgbox" I have a "Return res".
Then it won't work.
Re: Variable as variable name
Posted: 06 Dec 2019, 14:24
by boiler
return %res%
Edit: After testing, this is not working as I expected.
Re: Variable as variable name
Posted: 06 Dec 2019, 14:25
by Martin
I think I'm making a mistake, but I don't know where....
It wont work like this:
Code: Select all
QA := 000
WA := 123
GB := 222
LIST := "QA,WA,GB"
msgbox % test(LIST)[1]
test(LIST){
Global
res := {}
split := StrSplit(LIST, ",")
Loop % split.MaxIndex()
{
;res[A_index] := % %split[A_index]%
res[A_index] := % split[A_index]
}
return res
}
Re: Variable as variable name
Posted: 06 Dec 2019, 14:33
by Martin
I tried these below:
Code: Select all
return % %res%
return %res%
return res
Result: "QA", but not "000".
Re: Variable as variable name Topic is solved
Posted: 06 Dec 2019, 14:33
by teadrinker
Martin wrote: ↑It wont work like this
The only way:
Code: Select all
QA := "000"
WA := 123
GB := 222
LIST := "QA,WA,GB"
msgbox % test(LIST)[1]
test(LIST){
Global
res := {}
split := StrSplit(LIST, ",")
Loop % split.MaxIndex()
{
var := split[A_index]
res[A_index] := %var%
}
return res
}
Re: Variable as variable name
Posted: 06 Dec 2019, 14:37
by Martin
Works very well!
teadrinker thank you very much for your trick
!!
Re: Variable as variable name
Posted: 06 Dec 2019, 14:40
by boiler
Strange that you can't just
return %b% below to get both MsgBox's to say "999", but you have to take the step of assigning it to another variable before the return statement.
Code: Select all
MsgBox, % MyFunc()
MyFunc()
{
a := 999
b := "a"
MsgBox, % %b%
c := %b%
return c
}
Re: Variable as variable name
Posted: 06 Dec 2019, 14:43
by teadrinker
boiler wrote: ↑Strange that you can't just return %b% below to get both MsgBox's to say "999"
Return wrote:Known limitation: For backward compatibility and ease-of-use, the following two examples are functionally identical:
return MyVar
return %MyVar%
Re: Variable as variable name
Posted: 06 Dec 2019, 14:44
by boiler
Thanks.
Re: Variable as variable name
Posted: 06 Dec 2019, 15:06
by guest3456
yes, there is no way to do this without assigning to an intermediate variable
Re: Variable as variable name
Posted: 06 Dec 2019, 18:41
by Xtra
Code: Select all
MsgBox, % MyFunc()
MyFunc()
{
a := 999
b := "a"
MsgBox, % %b%
return (%b%)
}
Re: Variable as variable name
Posted: 06 Dec 2019, 18:56
by teadrinker
Yeah, also this works:
Code: Select all
MsgBox, % MyFunc()
MyFunc()
{
a := 999
b := "a"
MsgBox, % %b%
return %b% + 0
}
Re: Variable as variable name
Posted: 06 Dec 2019, 18:56
by boiler
Nice. Thanks.
Edit:
or return %b% ""
But return (%b%) looks the least kludgey.