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: FourthWorld, heatherlaine, Klaus, kevinmiller

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

Variable names in variables

Post by WaltBrown » Sat Sep 17, 2011 3:23 am

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: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Variable names in variables

Post by Klaus » Sat Sep 17, 2011 11:30 am

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
Location: NE USA

Re: Variable names in variables

Post by WaltBrown » Sat Sep 17, 2011 5:22 pm

Thanks. I had blanked on the "do" statement.
Walt
Walt Brown
Omnis traductor traditor

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7239
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Variable names in variables

Post by jacque » Sat Sep 17, 2011 8:29 pm

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
Location: NE USA

Re: Variable names in variables

Post by WaltBrown » Sun Sep 18, 2011 6:21 am

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

Return to “Getting Started with LiveCode - Complete Beginners”