Change variable name in a repeat loop

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
doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Change variable name in a repeat loop

Post by doobox » Wed Nov 06, 2013 9:56 am

Hi there,

I want to be able to change a variable name within a repeat loop if i can somehow.
I need to assign data to a a number of different variables so i can use them in an answer dialogue

Here is the bare bones basics of what i would like to do, but it see's that (tVar & x) a garbage, and not my expected , tVar1, tVar2, tVar3 as it loops through the repeat.

Code: Select all

   local tvar
   repeat with x = 1 to 3
      put 10 + x into (tvar & x)
   end repeat
   answer "a list of three choices" with tvar1 or tVar2 or tVar3 titled "A way to step var names"
Kind Regards
Gary

https://www.doobox.co.uk

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Change variable name in a repeat loop

Post by Klaus » Wed Nov 06, 2013 11:48 am

Hi Gary,

this is one of the cases where you need to DO something!
Do this, no need for an unused local:

Code: Select all

on mouseUp
   repeat with x = 1 to 3
     do ("put 10 + x into" && (tvar & x))
   end repeat
   answer "a list of three choices" with tvar1 or tVar2 or tVar3 titled "A way to step var names"
end mouseUp
Best

Klaus

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Change variable name in a repeat loop

Post by doobox » Wed Nov 06, 2013 12:19 pm

Ahhh.. Thanks a million Klaus. I never would have got that one on my own.

I only ever used "do" in the past like : do the text of field "x" as AppleScript
Things like that.

Now i read the docs for "do" it makes sense :-)
Kind Regards
Gary

https://www.doobox.co.uk

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Change variable name in a repeat loop

Post by dunbarx » Wed Nov 06, 2013 3:06 pm

"Do" is a powerful tool that forces an additional level of evaluation of an expression. It goes back 26 years.

It allows the engine to fully compose what you logically thought was a valid construct "tVar + x". I see that you were thorough in trying to help the parser by using parentheses, but there is a disconnect in how that parser can evaluate expressions of this type, where a "compound" variable is assembled in what seems to be a logical way, but is still a little too disparate for LC to assimilate.

Those are all high falutin' words that paraphrase what Danny Goodman said a generation ago, in HC, where this all originated. He said (paraphrased) that when you think you have built what seems to be a valid expression and it still does not work, try rewriting with a "do" construction, as the engine may need an additional level of evaluation to work properly.

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Change variable name in a repeat loop

Post by FourthWorld » Wed Nov 06, 2013 4:24 pm

Variables whose names can't be known in advance are a natural fit for arrays:

Code: Select all

local tVarA
repeat with x = 1 to 3
   put 10 + x into tVarA[x]
end repeat
answer "a list of three choices" with tVarA[1] or tVarA[2] or tVarA[3] titled "A way to step var names"
Much faster than "do", and much easier to debug than dynamically-generated expressions.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Change variable name in a repeat loop

Post by Klaus » Wed Nov 06, 2013 4:28 pm

Oh, yes, sure, I always forget about that one :D

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Change variable name in a repeat loop

Post by dunbarx » Wed Nov 06, 2013 6:01 pm

What Richard said. He has modernized much better than I have.

Just a note, though, since it is a good learning point, the expression "tVar + x" really ought to be "tVar & x". The former will throw an error in the "do" construction if tVar is not a number. I assume that you really wanted:

tVar1, tVar2, tVar3... (whatever tVar resolved to)

Craig

Post Reply