Indirect Referencing

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
vamp07
Posts: 21
Joined: Mon Apr 10, 2006 2:12 pm

Indirect Referencing

Post by vamp07 » Thu Mar 26, 2009 6:16 pm

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!!!

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Location: London, UK
Contact:

Post by Mark Smith » Thu Mar 26, 2009 6:33 pm

try

do "put item 1 of" && variableName

Best,

Mark

vamp07
Posts: 21
Joined: Mon Apr 10, 2006 2:12 pm

Post by vamp07 » Thu Mar 26, 2009 7:00 pm

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.

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Thu Mar 26, 2009 9:45 pm

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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

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

Post by SparkOut » Thu Mar 26, 2009 10:11 pm

Nice one Jan, you've killed two birds with one stone here. See http://forums.runrev.com/phpBB2/viewtop ... 2612#12612

Lynn P.
Posts: 79
Joined: Sun Apr 09, 2006 1:09 pm

Re: Indirect Referencing

Post by Lynn P. » Fri Mar 27, 2009 2:13 am

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

vamp07
Posts: 21
Joined: Mon Apr 10, 2006 2:12 pm

Post by vamp07 » Fri Mar 27, 2009 2:23 pm

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.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Fri Mar 27, 2009 4:12 pm

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.
Last edited by sturgis on Fri Mar 27, 2009 4:22 pm, edited 1 time in total.

vamp07
Posts: 21
Joined: Mon Apr 10, 2006 2:12 pm

Post by vamp07 » Fri Mar 27, 2009 4:16 pm

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.

Post Reply

Return to “Talking LiveCode”