Variable names in variables

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Variable names in variables

Post 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.
Walt Brown
Omnis traductor traditor
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Variable names in variables

Post 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
WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Re: Variable names in variables

Post by WaltBrown »

Thanks. I had blanked on the "do" statement.
Walt
Walt Brown
Omnis traductor traditor
jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7423
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Variable names in variables

Post 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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Re: Variable names in variables

Post 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.
Walt Brown
Omnis traductor traditor
Post Reply