Indirect reference to a variable
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Indirect reference to a variable
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.
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
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
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
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Indirect reference to a variable
This seems a natural fit for an array, allowing you to store any data associated with any arbitrary name, e.g.: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"?
put 2 into gMyGlobalArray[ tName ]
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Indirect reference to a variable
Richard-
But I think you meant

Brilliant bit of out-of-the-box thinking. I was going with Klaus on this, but you're right - an array is perfect here.This seems a natural fit for an array
But I think you meant
Code: Select all
put 3 into gMyGlobalArray[ tName ]

PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Indirect reference to a variable
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.
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
Maybe I'm missing something, but I fail to see how
is any more complicated than
Both will do the trick, of course, but I think the array form is more readable (and faster).
Code: Select all
put tLocalValue into gVarArray["gTest"]
Code: Select all
do ("put" && tLocaLValue && "into" && tName)
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Indirect reference to a variable
That was just a test of your attentionAtoZ 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)
...


Re: Indirect reference to a variable
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.)
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
Fair enough. But for reference, here's the entire code for what you're trying to do:trying to master a whole area of coding that I'm not very familiar with yet.
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
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)
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Indirect reference to a variable
Your life is about to change. Well, the scripting part of it, anyway.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.)

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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Indirect reference to a variable
Also... value () function is helpful here: http://forums.runrev.com/phpBB2/viewtop ... 2813#12813
Re: Indirect reference to a variable
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.
As usual, there's more than one way to do most anything in LiveCode. Glad to have another approach to solve this problem.