Page 1 of 1

Indirect Referencing

Posted: Thu Mar 26, 2009 6:16 pm
by vamp07
Hi guys,

I'm trying to reference the contents of a variable using its name in another variable. So far its not working. Any pointers? Here is an example of what I mean.

put "x,y,z" into var1
put "var1" into varibleName
put item 1 of varibleName


What I get in the message box is var1 but what I want is x.

Thanks!!!

Posted: Thu Mar 26, 2009 6:33 pm
by Mark Smith
try

do "put item 1 of" && variableName

Best,

Mark

Posted: Thu Mar 26, 2009 7:00 pm
by vamp07
Ahhhh.. That makes sense. Does anybody know of some other thread that already answers this question? I looked for "indirect" but could not find anything. Seems like something pretty common people would want to do.

Posted: Thu Mar 26, 2009 9:45 pm
by Janschenkel
You might also be interested in the value function.

Code: Select all

on mouseUp
  local x,y
  put 16 into x
  put "sqrt(x)" into y
  answer value(y)  -- should display 4
end mouseUp
HTH,

Jan Schenkel.

Posted: Thu Mar 26, 2009 10:11 pm
by SparkOut
Nice one Jan, you've killed two birds with one stone here. See http://forums.runrev.com/phpBB2/viewtop ... 2612#12612

Re: Indirect Referencing

Posted: Fri Mar 27, 2009 2:13 am
by Lynn P.
vamp07 wrote:Hi guys,

I'm trying to reference the contents of a variable using its name in another variable. So far its not working. Any pointers? Here is an example of what I mean.

put "x,y,z" into var1
put "var1" into varibleName
put item 1 of varibleName


What I get in the message box is var1 but what I want is x.

Thanks!!!
var1 is a variable so it doesn't need quotes. If you add quotes to it, Rev treats it as a text string.
Try:

Code: Select all

put "x,y,z" into var1
put var1 into varibleName
put item 1 of varibleName

Posted: Fri Mar 27, 2009 2:23 pm
by vamp07
var1 is a variable so it doesn't need quotes. If you add quotes to it, Rev treats it as a text string.
Try:
That would work but its not really indirect referencing.

Posted: Fri Mar 27, 2009 4:12 pm
by sturgis
edit: another kudo to mark. I only know about value() due to some Mark tips.


use the value function

Code: Select all

on mouseUp
   
put "x,y,z" into var1
put "var1" into varibleName
put item 1 of value(varibleName)
end mouseUp
This is the correct method to indirect reference yes?
Similar to eval in other languages.
vamp07 wrote:
var1 is a variable so it doesn't need quotes. If you add quotes to it, Rev treats it as a text string.
Try:
That would work but its not really indirect referencing.

Posted: Fri Mar 27, 2009 4:16 pm
by vamp07
I agree seems like value() is the most elegant way of doing this. I'm doing to go back and change my code from the do method.