Stuck in a loop?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Stuck in a loop?

Post by bjb007 » Sun Jun 22, 2008 4:09 pm

I have code like this

repeat with i = theTotal to 1 step -1
if z < 29 then
put hArray into uhArray[z]
add 1 to z
else
put theTotal into i
end if
end repeat

z = 1 at the start.
theTotal > 50

hArray contains a list of numbers (not
consecutive) and I want to get the last 28
unique numbers so that I can get the
unused numbers. (The code above doesn't
detect repeat numbers).

The question is "Why does the loop keep
repeating after theTotal is put into i?

It should come out of the repeat loop, I feel.
This is the only way I can see to exit the loop
without making it into a separate handler. It's
something I've used in other languages with no
problems.

Any ideas welcome including how to detect
duplicates and not put them into uhArray a
second time.

Perhaps a field is the answer but should be
able to do it with arrays.
Life is just a bowl of cherries.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Sun Jun 22, 2008 6:07 pm

The loop seems to repeats from theTotal down to 1, so it'll exit when x = 1 or less, not when it's the same as theTotal (it would exit immediately in that case).

I'd suggest using "exit repeat" in your if/else statement, that'll be easier to follow and cleaner to read. Barring that, set x to 1 and it'll stop repeating.

If you just care for the unique numbers, but not the order, then i suggest this:

Code: Select all

split listOfNumbers by return and tab --there can be no tab in listOfNumbers
put the keys of listOfNumbers into listOfNumbers
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Stuck in a loop?

Post by bjb007 » Mon Jun 23, 2008 2:57 am

Of course! Set x to 1 not to theTotal.

Can't understand how I missed finding
"exit repeat".

Unfortunately the order is important in
this case so really need to be able to
get the number of the array element
that a particular number's in.

Can't find a way to get at that.

Say theArray[3] = 29.
29 is the number I want.
How can I record that it's in element 3?

Edit...
Since what I'm looking for is the point
where a certain number of buttons
haven't been clicked I've come to the
conclusion that the only way is to go
from last click sending "mouseUp" to each
button in the list and checking the number
of unclicked buttons after each "mouseUp".

This is going in the reverse direction to
the one required.

When it gets to the point where the required
number of buttons are unclicked I can grab
the array element number and do the same
from there to the last click (i.e. in the right
order).

Might take longer but time isn't really important.
Life is just a bowl of cherries.

TEDennis
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 41
Joined: Tue Apr 11, 2006 12:36 am

Post by TEDennis » Mon Jun 23, 2008 4:19 pm

Try this ...

Code: Select all

on mouseUp
    put 19 into myArray[5]
    put 29 into myArray[4]
    put 39 into myArray[3]
    put 49 into myArray[2]
    put 59 into myArray[1]
    combine myArray by row
    put myArray & cr & 29 && "is in array element" && lineOffset(29, myArray) 
end mouseUp

Post Reply