PDA

View Full Version : skipping loop iterations


Blanks
02-05-2009, 07:49 AM
hi im trying to skip loop iterations in a for loop based on weather the value is a negative number or positive number (2,4,6,8,10.....100) but i cant figure out any way of doing it. Does anybody have any ideas

ZeBoxx2
02-05-2009, 08:15 AM
calling 'continue' within a loop will cause it to skip the remainder of the code and continue to the next loop iteration

if (number < 0) do ( continue )

groutcho
02-05-2009, 09:06 AM
if you need to test a number to see whether it is even or odd, you can do this

for LOOP do
(
if mod number 2 > 0 do (continue) --the number is odd
)

JHN
02-05-2009, 10:00 AM
And another one, skipping the if statement altogether:


for i = -50 to 100 where mod i 2 == 0 and i >= 0 do
(
print i
)


-Johan

ZeBoxx2
02-05-2009, 10:17 AM
well gosh.. if you need that, then why not 1. start your loop positive, at an even number and 2. use the 'by 2' statement in the loop so that it will never hit an odd number to begin with?

I thought this was for a more generic problem ;)

groutcho
02-05-2009, 10:22 AM
well gosh.. if you need that, then why not 1. start your loop positive, at an even number and 2. use the 'by 2' statement in the loop so that it will never hit an odd number to begin with?

I thought this was for a more generic problem ;)

Do you really need to break our dreams ? :cry:

ZeBoxx2
02-05-2009, 10:30 AM
Do you really need to break our dreams ? :cry:

well alright... how about replacing 'mod <number> 2 != 0' with 'bit.get <number> 1'? :thumbsup:

groutcho
02-05-2009, 12:09 PM
What the heck does "returns the boolean state of the least significant bit" mean ?

drdubosc
02-05-2009, 12:23 PM
It means ZB is a closet Perl Monk ! :rolleyes:

Piflik
02-05-2009, 01:57 PM
The Least significant bit (LSB) is the last number in an binary digit.

1001011010100 has LSB 0
1001011010101 has LSB 1

Every binary with LSB 1 is odd.

So bit.get 3 1 is true, bit.get 2 1 is false.

groutcho
02-05-2009, 02:08 PM
Thanks man :love:

Blanks
02-05-2009, 08:15 PM
Thanks for the examples.In the script im trying to implement it in, my array stores structures on the negative valued indexes and I will be calling a function to delete whats stored in structures members. so i could easily use the bit.get example to do this..........I dnt know why I cant get my head around CONTINUE and your first examples but i understand the last example. Would some care to change the following example to use CONTINUE instead of bit.get, just so i know how to use it

myArray = #("a","b","c","d","e","f")

for i = 1 to myArray.count where bit.get i 1 == true do
(print myArray[i])

ZeBoxx2
02-05-2009, 08:44 PM
maybe it's the hour of the day, but I'm entirely confused by what you just said :)

arrays don't have negative indexes (they all start at 1), so you already lost me at that point :>

If you have an array of structs....
and thos structs have a property that is a number...
and you want to have a new array with only those structs where that property is a positive number (nothing to do with odd (1, 3, 5, 7, 9) vs even (0, 2, 4, 6, 8))...


newArrayOfStructs = for myStruct in arrayOfStructs where (myStruct.propertyname > 0) collect ( myStruct )


e.g.


-- define an example struct
struct Hello_s (
somevalue
)
#Struct:Hello_s(
somevalue:<data>)

-- collect a few with random values, initialize the random seed
seed 0
OK
arrayOfStructs = for i = 1 to 10 collect ( Hello_s somevalue:(random -100 100) )
#((Hello_s somevalue:-47), (Hello_s somevalue:42), (Hello_s somevalue:-51), (Hello_s somevalue:28), (Hello_s somevalue:-35), (Hello_s somevalue:60), (Hello_s somevalue:-64), (Hello_s somevalue:72), (Hello_s somevalue:-25), (Hello_s somevalue:-60))

-- now let's collect just the positive ones into a new array
newArrayOfStructs = for myStruct in arrayOfStructs where (myStruct.somevalue > 0) collect ( myStruct )
#((Hello_s somevalue:42), (Hello_s somevalue:28), (Hello_s somevalue:60), (Hello_s somevalue:72))


( 'for <something> in <array>' is a special kind of loop that doesn't work a counter, it simply assigns each array element, in order, to the '<something>' variable for you to work with. 'where <condition>' basically acts as a filter, allowing you to only perform the code on elements where that condition is true )

Blanks
02-05-2009, 09:23 PM
sorry if im confusing you. My array sequence looks like this #(<struct>, <string>, <struct>, <string>,............<struct>, <string>) and i just need to call a function on the structs.i suck at explaining stuff

Piflik
02-05-2009, 09:32 PM
Would that work?


For i in 1 to myArray.count where classoff myArray[i] == struct do ()

ZeBoxx2
02-05-2009, 09:34 PM
ahh, then yes, just go...


-- if the structs are in 1, 3, 5, 7, 9
for i = 1 to <array>.count by 2 do (
-- do something with <array>[i]
)

-- if the structs are in 2, 4, 6, 8, 10
for i = 2 to <array>.count by 2 do (
-- do something with <array>[i]
)

-- if you want to make it slightly more secure..
for someElement in <array> where (classOf someElement != string) do (
-- do something with someElement
)

Blanks
02-05-2009, 10:21 PM
lol i cant believe i forgot by 2.........I had something like this after you lot helped


myStructArray = #() -- stores myStructs and myStruct.cName......#(<myStruct>,<myStruct.cName>,<myStruct>,<myStruct.cName>,.......)

myString = "I am dumb" -- in my actual script this is an item selected from a dropDown list

struct myStruct
(
targetCam, camTargetObject, cCircle, cName
)


-- deletes myStruct.targetCam, myStruct.camTargetObject, myStruct.cCircle from the struct where cName== myString
fn deleteMyStructObjects =
(
for i = 1 to myArray.count where bit.get i 1 == true AND myArray[i].cName == myString do
(
delete myArray[i].targetCam
delete myArray[i].camTargetObject
delete myArray[i].cCircle
)
)

The thing is I actual just realised i dont even need to store cName in myArray. I could just iterate through the structs and find out which ones cName == myString

I think i need some sleep!!!:banghead:

Sorry for wasting your time but thanks for the help

CGTalk Moderation
02-05-2009, 10:21 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.