Variable as variable name Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Martin
Posts: 26
Joined: 24 Jun 2017, 02:54

Variable as variable name

06 Dec 2019, 12:00

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?
Martin
Posts: 26
Joined: 24 Jun 2017, 02:54

Re: Variable as variable name

06 Dec 2019, 13:52

Unfortunately, it doesn't work :(
Its the same..
teadrinker
Posts: 1175
Joined: 29 Mar 2015, 09:41
Contact:

Re: Variable as variable name

06 Dec 2019, 14:03

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
}
Martin
Posts: 26
Joined: 24 Jun 2017, 02:54

Re: Variable as variable name

06 Dec 2019, 14:14

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.
User avatar
boiler
Posts: 3467
Joined: 21 Dec 2014, 02:44

Re: Variable as variable name

06 Dec 2019, 14:24

return %res%

Edit: After testing, this is not working as I expected.
Last edited by boiler on 06 Dec 2019, 14:30, edited 1 time in total.
Martin
Posts: 26
Joined: 24 Jun 2017, 02:54

Re: Variable as variable name

06 Dec 2019, 14:25

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
}
Martin
Posts: 26
Joined: 24 Jun 2017, 02:54

Re: Variable as variable name

06 Dec 2019, 14:33

I tried these below:

Code: Select all

	return % %res%
	return %res%
	return res
Result: "QA", but not "000".
Last edited by Martin on 06 Dec 2019, 14:34, edited 1 time in total.
teadrinker
Posts: 1175
Joined: 29 Mar 2015, 09:41
Contact:

Re: Variable as variable name  Topic is solved

06 Dec 2019, 14:33

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
}
Martin
Posts: 26
Joined: 24 Jun 2017, 02:54

Re: Variable as variable name

06 Dec 2019, 14:37

Works very well!
teadrinker thank you very much for your trick :thumbup: !!
User avatar
boiler
Posts: 3467
Joined: 21 Dec 2014, 02:44

Re: Variable as variable name

06 Dec 2019, 14:40

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
}
teadrinker
Posts: 1175
Joined: 29 Mar 2015, 09:41
Contact:

Re: Variable as variable name

06 Dec 2019, 14:43

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%
guest3456
Posts: 2991
Joined: 09 Oct 2013, 10:31

Re: Variable as variable name

06 Dec 2019, 15:06

yes, there is no way to do this without assigning to an intermediate variable

User avatar
Xtra
Posts: 1598
Joined: 02 Oct 2015, 12:15

Re: Variable as variable name

06 Dec 2019, 18:41

Code: Select all

MsgBox, % MyFunc()

MyFunc()
{
    a := 999
    b := "a"

    MsgBox, % %b%
    return (%b%)
}
teadrinker
Posts: 1175
Joined: 29 Mar 2015, 09:41
Contact:

Re: Variable as variable name

06 Dec 2019, 18:56

Yeah, also this works:

Code: Select all

MsgBox, % MyFunc()

MyFunc()
{
    a := 999
    b := "a"

    MsgBox, % %b%
    return %b% + 0
}
User avatar
boiler
Posts: 3467
Joined: 21 Dec 2014, 02:44

Re: Variable as variable name

06 Dec 2019, 18:56

Nice. Thanks.

Edit:
or return %b% ""

But return (%b%) looks the least kludgey.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], grimboto and 50 guests