Newbie: Marco/Eval question

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kpeters
Posts: 112
Joined: Wed Mar 21, 2007 9:32 pm

Newbie: Marco/Eval question

Post by kpeters » Sat May 05, 2007 12:43 am

I do hope something like this can be done in Transcript:

Code: Select all

local p1, p2, p3 -- 3 arrays
--
repeat with index = 1 to 3 
  put index into < something to eval p + index? >[ "key1" ]
end repeat 
What is the syntax?

TIA,
Kai
Last edited by kpeters on Sat May 05, 2007 4:24 am, edited 1 time in total.

kpeters
Posts: 112
Joined: Wed Mar 21, 2007 9:32 pm

Post by kpeters » Sat May 05, 2007 4:24 am

No wonder I was confused - apparently there's a bug (since 2.5.xx) in Revolution that makes things a bit harder - but this works:

Code: Select all

repeat with index = 1 to 3
   put <whatever its is you want to stash in the array> into tmp
   put ( "put tmp into g" & index & "[" & quote & "<key goes here>" & quote & "]" ) into code
  do code
end repeat 
Kai

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

Post by Mark Smith » Sat May 05, 2007 7:12 pm

kpeters wrote:No wonder I was confused - apparently there's a bug (since 2.5.xx) in Revolution that makes things a bit harder - but this works:

Code: Select all

repeat with index = 1 to 3
   put <whatever its is you want to stash in the array> into tmp
   put ( "put tmp into g" & index & "[" & quote & "<key goes here>" & quote & "]" ) into code
  do code
end repeat 
Kai
Kai, I don't think this is a bug, it's just the way that you deal indirectly with variables in Revolution.
Incidentally, it's not actually necessary to bundle the 'do' statement up into a variable, you could just:

Code: Select all

do "put tmp into g" & index & "[" & quote & "<key goes here>" & quote & "]" 
An alternative would be to use a multi-keyed array:

Code: Select all

local theArray
repeat with index = 1 to 3
  put <whatever> into theArray[n,"someKey"]
end repeat
Which may or may not suit your purpose.

Best,

Mark

xApple
Posts: 113
Joined: Wed Nov 29, 2006 10:21 pm

Post by xApple » Sat May 05, 2007 7:31 pm

Mark Smith wrote:

Code: Select all

do "put tmp into g" & index & "[" & quote & "<key goes here>" & quote & "]" 
That can be writen better using format():

Code: Select all

do format("put tmp into g%s["key"]",index)
Or maybe even better using merge():

Code: Select all

do merge("put tmp into g[[index]][key]")

kpeters
Posts: 112
Joined: Wed Mar 21, 2007 9:32 pm

Post by kpeters » Sat May 05, 2007 10:38 pm

Thanks guys -

as a newbie I do appreciate your suggestions on code improvement!

Mark - as for the suspected bug look here:

http://article.gmane.org/gmane.comp.ide ... ser/56411/
match=eval

Kai

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun May 06, 2007 9:33 am

Kai,

There are many ways to do things in Revolution. Rather than creating variables from strings, for which you need the do or merge commands, you could use custom properties. Here's an example:

Code: Select all

on mouseUp
  put empty -- clear msg box
  put "Test" into myVar
  -- store
  repeat with index = 1 to 3
    put "cProp" & myVar & "[" & index & "]" into myVarName
    set the myVarName of me to random(10)
  end repeat
  -- display in msg box
  repeat with index = 1 to 3
    put "cProp" & myVar & "[" & index & "]" into myVarName
    put the myVarName of me & cr after msg
  end repeat
end mouseUp
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by Mark Smith » Mon May 07, 2007 2:15 am

kpeters wrote:Thanks guys -

as a newbie I do appreciate your suggestions on code improvement!

Mark - as for the suspected bug look here:

http://article.gmane.org/gmane.comp.ide ... ser/56411/
match=eval

Kai
Kai, this turned out to be a documentation bug...it's actually always been necessary to use do/merge to do what you've described.

Also, using format is a cool idea that I'd not come across before, so thanks to Mark Schonewille for that!

Best,

Mark Smith

Post Reply