Indirect reference to a variable

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
AtoZ
Posts: 64
Joined: Tue Mar 06, 2012 8:31 am

Indirect reference to a variable

Post by AtoZ » Tue Sep 04, 2012 9:11 am

If I have the name of a global stored in a variable (for example, a local variable, tName, contains "gTest" which is the name of a declared global in my stack), is there some command (or perhaps function that I could write) that would allow me to store info in the global by referring to it using the local variable.

In other words, what would be the LiveCode equivalent of "put 3 into the container whose name is stored in tName"?

I've tried looking this up in various LiveCode reference materials, but I guess I don't know the correct words to search for. (It's sort of like trying to look up how to spell the word "pneumonia" in the dictionary, you won't get anywhere if you look in the n's.)

Any suggestions would be much appreciated.
Using LiveCode 5.5 on Windows 7 Home Premium Edition, Service Pack 1

Image
On my way from A TO Z, I often end up AT OZ.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Indirect reference to a variable

Post by Klaus » Tue Sep 04, 2012 11:44 am

Hi AtoZ,

yes, this is DOable with the "do" command :D

Example:
...
global gGlobal
put 3 into tLocaLValue

## Create a string that you can DO afterwards
put "gGlobal" into tName
do ("put && tLocaLValue && "into" && tName)

answer gGlobal
## gGLobal = 3
...

Tested and works :)


Best

Klaus

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Indirect reference to a variable

Post by FourthWorld » Tue Sep 04, 2012 4:15 pm

AtoZ wrote:If I have the name of a global stored in a variable (for example, a local variable, tName, contains "gTest" which is the name of a declared global in my stack), is there some command (or perhaps function that I could write) that would allow me to store info in the global by referring to it using the local variable.

In other words, what would be the LiveCode equivalent of "put 3 into the container whose name is stored in tName"?
This seems a natural fit for an array, allowing you to store any data associated with any arbitrary name, e.g.:

put 2 into gMyGlobalArray[ tName ]
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Indirect reference to a variable

Post by mwieder » Tue Sep 04, 2012 5:31 pm

Richard-
This seems a natural fit for an array
Brilliant bit of out-of-the-box thinking. I was going with Klaus on this, but you're right - an array is perfect here.
But I think you meant

Code: Select all

put 3 into gMyGlobalArray[ tName ]
:D

AtoZ
Posts: 64
Joined: Tue Mar 06, 2012 8:31 am

Re: Indirect reference to a variable

Post by AtoZ » Tue Sep 04, 2012 5:33 pm

Thanks guys for the quick responses (as usual).

Richard, you're probably right about using arrays, except in this case I only need to do this a couple of times, so I think Klause's solution is easiest for my situation.

For the benifit of anyone else might want to do this: There was a small typo in this line of the proposed code:

do ("put && tLocaLValue && "into" && tName)

A quote mark is missing after the word "put". The line should read:

do ("put" && tLocaLValue && "into" && tName)

This is exactly what I needed. Thanks again.
Using LiveCode 5.5 on Windows 7 Home Premium Edition, Service Pack 1

Image
On my way from A TO Z, I often end up AT OZ.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Indirect reference to a variable

Post by mwieder » Tue Sep 04, 2012 5:41 pm

Maybe I'm missing something, but I fail to see how

Code: Select all

put tLocalValue into gVarArray["gTest"]
is any more complicated than

Code: Select all

do ("put" && tLocaLValue && "into" && tName) 
Both will do the trick, of course, but I think the array form is more readable (and faster).

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Indirect reference to a variable

Post by Klaus » Tue Sep 04, 2012 6:50 pm

AtoZ wrote:...
For the benifit of anyone else might want to do this: There was a small typo in this line of the proposed code:
do ("put && tLocaLValue && "into" && tName)
A quote mark is missing after the word "put". The line should read:
do ("put" && tLocaLValue && "into" && tName)
...
That was just a test of your attention 8) :)

AtoZ
Posts: 64
Joined: Tue Mar 06, 2012 8:31 am

Re: Indirect reference to a variable

Post by AtoZ » Tue Sep 04, 2012 10:30 pm

mwieder,

Yes, you're no doubt right -- as long as you are comfortable working with arrays. For my part, I haven't had occasion to work much with arrays (although it's on my list), so in this case since I only need to use the "do" command a couple of times, it's simpler to do that than trying to master a whole area of coding that I'm not very familiar with yet.

Thanks, everyone, for your help. I have no doubt that I'll be back with more questions when I do start trying to work with arrays (although I'm sure I'll be back even sooner with questions on other topics -- smile.)
Using LiveCode 5.5 on Windows 7 Home Premium Edition, Service Pack 1

Image
On my way from A TO Z, I often end up AT OZ.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Indirect reference to a variable

Post by mwieder » Tue Sep 04, 2012 10:59 pm

trying to master a whole area of coding that I'm not very familiar with yet.
Fair enough. But for reference, here's the entire code for what you're trying to do:

Code: Select all

put 3 into tLocalValue
-- put value into variable "gTest"
put tLocalValue into gVarArray["gTest"]

-- retrieve value from variable "gTest"
put gVarArray["gTest"] into tLocalValue
It's the equivalent for

Code: Select all

put 3 into tLocalValue
put "gTest" into tName
-- put value into variable "gTest"
do ("put" && tLocaLValue && "into" && tName)

-- retrieve value from variable "gTest"
do ("put" && tName && "into" && tLocaLValue)
If you really want to get fancy and generic about it (and make it look pretty much the same as the "do" form) you could say

Code: Select all

put 3 into tLocalValue
put "gTest" into tName
 -- put value into variable "gTest"
put tLocalValue into gVarArray[tName]

-- retrieve value from variable "gTest"
put gVarArray[tName] into tLocalValue

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Indirect reference to a variable

Post by FourthWorld » Tue Sep 04, 2012 11:28 pm

AtoZ wrote:Thanks, everyone, for your help. I have no doubt that I'll be back with more questions when I do start trying to work with arrays (although I'm sure I'll be back even sooner with questions on other topics -- smile.)
Your life is about to change. Well, the scripting part of it, anyway. :)

Arrays are so useful for so many things, once you get the hang of them you'll wonder how you lived without them.

They're almost as useful as custom properties - and often use very similar syntax, so it's a two-fer-one on the learning curve. :)

Keep us posted as you have questions.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Indirect reference to a variable

Post by SparkOut » Sun Sep 16, 2012 2:04 pm

Also... value () function is helpful here: http://forums.runrev.com/phpBB2/viewtop ... 2813#12813

AtoZ
Posts: 64
Joined: Tue Mar 06, 2012 8:31 am

Re: Indirect reference to a variable

Post by AtoZ » Sun Sep 16, 2012 8:37 pm

Thanks SparkOut,

As usual, there's more than one way to do most anything in LiveCode. Glad to have another approach to solve this problem.
Using LiveCode 5.5 on Windows 7 Home Premium Edition, Service Pack 1

Image
On my way from A TO Z, I often end up AT OZ.

Post Reply