Page 1 of 1

Newbie: Marco/Eval question

Posted: Sat May 05, 2007 12:43 am
by kpeters
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

Posted: Sat May 05, 2007 4:24 am
by kpeters
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

Posted: Sat May 05, 2007 7:12 pm
by Mark Smith
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

Posted: Sat May 05, 2007 7:31 pm
by xApple
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]")

Posted: Sat May 05, 2007 10:38 pm
by kpeters
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

Posted: Sun May 06, 2007 9:33 am
by Mark
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

Posted: Mon May 07, 2007 2:15 am
by Mark Smith
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