Page 1 of 1

Variable names in variables

Posted: Sat Sep 17, 2011 3:23 am
by WaltBrown
I know I've asked this before, but can't recall the answer.

If I have a local variable that contains a string which is the name of another variable, how do I get at the contents of the inner variable?

put 5 into variable1
put "variable1" into variable2

I want to get "5" from variable2. I tried value(variable2), but that of course gives me "variable1", unless it is a system global preceded with a dollar sign, as in:

put "$APPDATA" into variable2

value(variable2) returns "C:\Users\Walt\AppData\Roaming".

Thanks. I know this is a "Well dur" question, I'm just drawing a blank.

Re: Variable names in variables

Posted: Sat Sep 17, 2011 11:30 am
by Klaus
Hi Walt,

although holding variable names in other variables is not really recommended unless avoidably,
you can make this via a DO statement!
...
put 5 into variable1
put "variable1" into variable2
do ("put" && variable2 && "into variable3")
put variable3
## Will put 5 into variable3
...
A bit cumbersome, but works :D


Best

Klaus

Re: Variable names in variables

Posted: Sat Sep 17, 2011 5:22 pm
by WaltBrown
Thanks. I had blanked on the "do" statement.
Walt

Re: Variable names in variables

Posted: Sat Sep 17, 2011 8:29 pm
by jacque
I don't think you need "do". This works for me:

on testvar
put 5 into var1
put var1 into var2
put var2
end testvar

Leave off the quotes and the value is used instead of the variable name.

Re: Variable names in variables

Posted: Sun Sep 18, 2011 6:21 am
by WaltBrown
Thanks Jacque. I'm not just transfering contents between two variables though, I want to send the name of the variable to another script for parsing.