Notifications
Clear all

[Closed] skipping loop iterations

Thanks man :love:

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])

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 )

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

Would that work?


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

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
)

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

Page 2 / 2