Page 1 of 1
Change variable name in a repeat loop
Posted: Wed Nov 06, 2013 9:56 am
by doobox
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"
Re: Change variable name in a repeat loop
Posted: Wed Nov 06, 2013 11:48 am
by Klaus
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
Re: Change variable name in a repeat loop
Posted: Wed Nov 06, 2013 12:19 pm
by doobox
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

Re: Change variable name in a repeat loop
Posted: Wed Nov 06, 2013 3:06 pm
by dunbarx
"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
Re: Change variable name in a repeat loop
Posted: Wed Nov 06, 2013 4:24 pm
by FourthWorld
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.
Re: Change variable name in a repeat loop
Posted: Wed Nov 06, 2013 4:28 pm
by Klaus
Oh, yes, sure, I always forget about that one

Re: Change variable name in a repeat loop
Posted: Wed Nov 06, 2013 6:01 pm
by dunbarx
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