Page 1 of 1
Indirect reference to a variable
Posted: Tue Sep 04, 2012 9:11 am
by AtoZ
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.
Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 11:44 am
by Klaus
Hi AtoZ,
yes, this is DOable with the "do" command
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
Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 4:15 pm
by FourthWorld
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 ]
Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 5:31 pm
by mwieder
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 ]

Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 5:33 pm
by AtoZ
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.
Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 5:41 pm
by mwieder
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).
Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 6:50 pm
by Klaus
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

Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 10:30 pm
by AtoZ
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.)
Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 10:59 pm
by mwieder
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
Re: Indirect reference to a variable
Posted: Tue Sep 04, 2012 11:28 pm
by FourthWorld
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.
Re: Indirect reference to a variable
Posted: Sun Sep 16, 2012 2:04 pm
by SparkOut
Re: Indirect reference to a variable
Posted: Sun Sep 16, 2012 8:37 pm
by AtoZ
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.