Using loop to change variable container

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
Josh
Posts: 46
Joined: Sat Jan 12, 2008 6:42 am

Using loop to change variable container

Post by Josh » Fri Dec 28, 2012 3:16 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Using loop to change variable container

Post by jmburnod » Fri Dec 28, 2012 3:32 pm

Hi Josh,
I think you can use

Code: Select all

do "put stim & i into container"
instead

Code: Select all

put "stim" & i into container 
Best regards
Jean-Marc
https://alternatic.ch

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

Re: Using loop to change variable container

Post by dunbarx » Fri Dec 28, 2012 4:30 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Using loop to change variable container

Post by sturgis » Fri Dec 28, 2012 5:05 pm

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

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

Re: Using loop to change variable container

Post by dunbarx » Fri Dec 28, 2012 7:42 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Using loop to change variable container

Post by sturgis » Fri Dec 28, 2012 10:15 pm

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.

Josh
Posts: 46
Joined: Sat Jan 12, 2008 6:42 am

Re: Using loop to change variable container

Post by Josh » Sun Dec 30, 2012 2:38 am

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.

Post Reply