variable names in other variables

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
WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

variable names in other variables

Post by WaltBrown » Fri May 15, 2009 12:34 am

Sorry, I saw the answer to this or a stack here somewhere and now cannot find it.

How can I get the value of say a property whose name is in a variable, as in:

put "backcolor" into tString
answer tString with "OK"

This returns "backcolor", but what I want is it to return the backcolor property value, like "white" or "#FFFFFF".

Thanks, Walt
Walt Brown
Omnis traductor traditor

acidjazz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 93
Joined: Mon Feb 16, 2009 2:37 am

Post by acidjazz » Fri May 15, 2009 1:41 am

Walt,
Let's say it's a field you're interested in...

Code: Select all

on mouseUp
   put the backgroundcolor of field 1 into tString
   answer tString
end mouseUp
If there is no background color set, the answer box will be completely blank. If there is a color, you'll get the RGB values for the color.
Of course, you don't need the intervening variable to get the values...

Code: Select all

on mouseUp
   answer the backgroundcolor of field 1
end mouseUp
Cheers,
Mark P.

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

Post by Janschenkel » Fri May 15, 2009 6:29 am

The do command and the value function can be used to dynamically execute commands or evaluate expressions.

Code: Select all

on mouseUp
  -- example of value function
  put 9 into x
  put "sqrt(x)" into y
  answer value(y)
  -- example of do command
  put "answer value(y)" into z
  do z
end mouseUp
So in your case it could be something like:

Code: Select all

on mouseUp
  put "backColor" into tPropertyName
  put ("answer the " & tPropertyName & " of me") into tCommand
  do tCommand
end mouseUp
The merge function can also save some time and make assembling such dynamic expressions and command a lot mpore readable:

Code: Select all

on mouseUp
  put "backColor" into tPropertyName
  do merge("answer the [[tPropertyName]] of me")
end mouseUp
For some examples of what you can accomplish using the merge function, read these newsletter articles: HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

acidjazz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 93
Joined: Mon Feb 16, 2009 2:37 am

Post by acidjazz » Fri May 15, 2009 7:03 am

Jan,
Very clever. It makes more sense to "do" it dynamically. (Sorry, I couldn't help myself) :)
Mark P.

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

Post by SparkOut » Fri May 15, 2009 11:42 am

Just a word about "the Power of Merge" - these are a series of incredibly useful articles which will help you get to grips with the concept of streamlining the dynamic code replacement. Very good stuff - only the online newsletter page has rendered the html entity " as a literal in the code boxes, and not applied the view as a " character.
This makes it a little confusing as Jan mentions that one of the advantages is to make the code more readable - but the rendering of " as " in the newsletter page actually makes it seem worse. Please don't get confused by this - it's not bad writing or confusing information from Jan, if you replace " with " then it will all become a lot clearer.

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

Post by Janschenkel » Fri May 15, 2009 1:31 pm

Wrt the " in the article: according to Heather, it was a deficiency in the code-to-html converter they used for tyhat article; but as this is in the process of conversion to a CMS, it should be rectified eventually.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Post by WaltBrown » Fri May 22, 2009 12:18 am

Thanks! Great stuff!
Walt Brown
Omnis traductor traditor

Post Reply