Trouble using repeat with loops and a counter variable

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
JeffO2013
Posts: 17
Joined: Thu Jun 27, 2013 7:41 pm

Trouble using repeat with loops and a counter variable

Post by JeffO2013 » Mon Feb 03, 2014 7:56 pm

Hi all,

I'm working on making a repeat loop to put items into an array. I'm using a repeat with loop and want to use the counter variable to also tell the program what key to store the data in within the array.

It will look something like this:

Code: Select all

put 1 into y

repeat with y = 1 to 12
   put any line of tCurrFiles into targetArray[y]
   delete current line of tCurrFiles
   add 1 to y
end repeat
I've tested that tCurrFiles actually has the files I want stored within it and that it will put a line from tCurrFiles into targetArray[1] and then show that file from targetArray[1] in an image. See below for how I tested this.

Code: Select all

put any line of tCurrFiles into targetArray[1]
delete current line of tCurrFiles
set the filename of img "Image 1" to targetArry[1]
This results in a picture from the folder I pass the program from an earlier set of code being displayed in "Image 1" and it does change every time I run this test.

I guess my question is does an array (or other data and control structures; I've had a similar problem with a get random type structure) use the x in the same way it does the one--as in it reads the x as the key--or will it use the data stored within x as the key--in this case the numbers 1 through 12?

Thank you so much for your help!

Jeff

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Trouble using repeat with loops and a counter variable

Post by Simon » Mon Feb 03, 2014 8:45 pm

Hi Jeff,
I'm not seeing that

Code: Select all

delete current line of tCurrFiles
works at all, in the dictionary "current" only applies to stacks and cards.

Aside from that I think I understand your question and yes, x or y or any var will have it's contents read as the value.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Trouble using repeat with loops and a counter variable

Post by dunbarx » Mon Feb 03, 2014 9:49 pm

Hi.

Something like this ought to get you going. Make a small field (field 1) with several short lines of text in it. Make a button and put this into its script:

Code: Select all

on mouseUp
   put fld 1 into temp
   repeat until temp = ""
      put random(the number of lines of temp) into currentLine
      put line currentLine of temp & return after tResults
      delete line currentLine of temp
   end repeat
   answer tResults
end mouseUp
There are other ways to do this, and you should try to think of some. The important thing is to see what is going on. Can you step through this script and monitor the variables?

Craig Newman

JeffO2013
Posts: 17
Joined: Thu Jun 27, 2013 7:41 pm

Re: Trouble using repeat with loops and a counter variable

Post by JeffO2013 » Thu Feb 06, 2014 8:12 pm

Sorry for the delayed reply. Today was the first day I've had to work on it since I asked my question.

I figured it out. I'm guessing the repeat loop automatically adds 1 to y (or whatever variable I'm using to keep track of the count)? I think my "add 1 to y" line was what was messing me up on the repeat.

Here is the code I ended up using to fix my problem:

Code: Select all

repeat with y = 1 to 12
      put random(the number of lines of tCurrFiles) into randomFile
      put line randomFile of tCurrFiles into targetArray[y]
      delete line randomFile of tCurrFiles
      put tCurrFiles into field "List 1"
      wait 1 second
   end repeat
The last two lines in the repeat were so I could see that the lines were actually being deleted.

Thank you both for your help!

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

Re: Trouble using repeat with loops and a counter variable

Post by dunbarx » Thu Feb 06, 2014 9:53 pm

Hi.

Lots of ways to do things in LC. This is what makes it so endearing.

Yes, if you use the "repeat with.." form you really should never mess with the local index variable. The engine does the automatic incrementing (or decrementing) and hates any outside interference. There are lots of other repeat structures that do call for that sort of thing, and they can be used as needed.

The only other comment I might make is that, unless you have a particular reason, use a property instead of an explicit value to set the index. You used "12". Well and good, unless that value changes, and then you have a maintenance problem. So do be aware that such lines as:

repeat with y = 1 to the number of lines of currFiles

Are likely more robust. In other words, wherever possible, let the engine do it...

Craig

Post Reply