Page 1 of 1
Using loop to change variable container
Posted: Fri Dec 28, 2012 3:16 pm
by Josh
Hi all,
I want to put a value into a particular variable that is based on loop iteration. So:
repeat with i = 1 to numStimuli
put line i of currentStimuli into tempStimuli
put "stim" & i into container --so this would set up the variable names "stim1", "stim2", and so on
put item 1 of line tempStimuli of totalList into container --Ideally, this would put the specified value into the variable "stim2", but it actually just replaces the variable name. Is there a workaround?
end repeat
I know it's not concise, but my current plan will be a series of "if" statements
if i = 1 then do XXXX
if i = 2 then do XXXX
and so on. Any input would be appreciated.
Josh
Re: Using loop to change variable container
Posted: Fri Dec 28, 2012 3:32 pm
by jmburnod
Hi Josh,
I think you can use
instead
Best regards
Jean-Marc
Re: Using loop to change variable container
Posted: Fri Dec 28, 2012 4:30 pm
by dunbarx
A bit more about what Jean-Marc said:
As a test, make a field named "testField". Put six short lines of data into it. In a button, place this in its script:
Code: Select all
on mouseUp
repeat with i = 1 to 6
put line i of fld "testField" into temp
do "put temp into stim" & i
end repeat
end mouseUp
Place a breakpoint into the "end mouseUp" line, and examine the contents of "sim1", sim2", etc.
This is a simple example of the use of the "do" construction, where an extra level of evaluation in embedded into a line of code, and is able to create, in this instance, named variables on the fly. This is possible with multiple lines of code as well, where you create the numbered variables first and then load them, but the "do" method is more compact. It bears experimenting with, as there are several valid and also invalid ways of preparing these statements. Practice is the only way.
Now try this in the button script:
Code: Select all
on mouseUp
repeat with i = 1 to 6
put line i of fld "testField" into temp
do "put" && temp && "into stim" & i
end repeat
end mouseUp
This also works, and I can only tell you that you should examine both, because as these statements get more complex, the second form will work where the first will not. This is a mystery. In general, I think, separating declared variables is stronger than including them into quoted fragments. Practice...
Craig Newman
Re: Using loop to change variable container
Posted: Fri Dec 28, 2012 5:05 pm
by sturgis
Why not just use an array?
Code: Select all
repeat with i = 1 to numStimuli
put item 1 of line i of currentStimuli into sStimA[i]
end repeat
Then sStimA[4] would contain the 4th stim from the loop.
The other methods are good to know, but an array is so perfect for this it doesn't make much sense to do it differently.
EDIT: In either case, if you have quite a few lines to go through using the 'repeat for each' type of loop will be tremendously faster.
Josh wrote:Hi all,
I want to put a value into a particular variable that is based on loop iteration. So:
repeat with i = 1 to numStimuli
put line i of currentStimuli into tempStimuli
put "stim" & i into container --so this would set up the variable names "stim1", "stim2", and so on
put item 1 of line tempStimuli of totalList into container --Ideally, this would put the specified value into the variable "stim2", but it actually just replaces the variable name. Is there a workaround?
end repeat
I know it's not concise, but my current plan will be a series of "if" statements
if i = 1 then do XXXX
if i = 2 then do XXXX
and so on. Any input would be appreciated.
Josh
Re: Using loop to change variable container
Posted: Fri Dec 28, 2012 7:42 pm
by dunbarx
Sturgis.
Absolutely. A perfect fit.
But this is a new liveCoder, and he should learn the old fashioned before the new.
That said, Josh, do you see what Sturgis is up to? Arrays arrange data and their associated "names" in a natural way via their internal structure. But you do need to become proficient with loops, so do it both ways if you want, but certainly do the loop.
Craig
Re: Using loop to change variable container
Posted: Fri Dec 28, 2012 10:15 pm
by sturgis
Absolutely, though, wasn't avoiding the loop, was avoiding the "do." Do can do very neat things, and understanding when you pre-evaluate variables and when you let "do" evaluate them inline, while confusing at first, is the biggest hurdle to grasping how do works. As you say, learning the ins and outs of 'do' is a good thing to do.
Re: Using loop to change variable container
Posted: Sun Dec 30, 2012 2:38 am
by Josh
Thanks so much, guys! I will indeed take note of each of these approaches and practice with them. Much more elegant than my clumsy approach.