PDA

View Full Version : increment nested loop parent


em3
06-29-2011, 05:15 AM
Hello!

I have a script that is like this


for i = 0 to myParent-1 do --there is 1 parent
(
getChildCount
for j in childCount-1 do --there are 6 children
(

if myThing(j).name == "hello, " then myVal1 == j.name
if myThing(j).name == "you" then myVal2 == j.name
if myThing(j).name == "are" then myVal3 == j.name
if myThing(j).name == "slow at this" then myVal4 == j.name

if myVal1 !=undefined and myVal2 !=undefined and myVal3 !=undefined and myVal4 !=undefined then print "hello"
)
)


it prints hello 3 times because of the 6 children, which is 2 times too many. I would like the parent to increment when all the values have been satisfied. Is there some code for "goto next parent item" I can use here?

Thanks!

I eagerly await denisT to turn this into one line of code... :thumbsup:

elT
06-29-2011, 06:21 AM
You need to break the second loop at that point so just type "exit" where you need it.
That way, you return to the first loop and go at it again.

if myVal1 !=undefined and myVal2 !=undefined and myVal3 !=undefined and myVal4 !=undefined then print "hello" else exit

em3
06-29-2011, 06:34 AM
that's it! Thank you, elT!

denisT
06-29-2011, 10:07 AM
Hello!

I have a script that is like this


for i = 0 to myParent-1 do --there is 1 parent
(
getChildCount
for j in childCount-1 do --there are 6 children
(

if myThing(j).name == "hello, " then myVal1 == j.name
if myThing(j).name == "you" then myVal2 == j.name
if myThing(j).name == "are" then myVal3 == j.name
if myThing(j).name == "slow at this" then myVal4 == j.name

if myVal1 !=undefined and myVal2 !=undefined and myVal3 !=undefined and myVal4 !=undefined then print "hello"
)
)


it prints hello 3 times because of the 6 children, which is 2 times too many. I would like the parent to increment when all the values have been satisfied. Is there some code for "goto next parent item" I can use here?

Thanks!

I eagerly await denisT to turn this into one line of code... :thumbsup:

it's pseudo code, isn't it?
but...
when you have multiple choice condition is better to use case expression:

case myThing(j).name of
(
"hello": ...
...
)

if one condition is right you don't need to check another
if a condition might match several patterns i would do it this way:

patterns = #(a, b, c, ...)
matched = undefined
for p in patterns while matched == undefined where my_value <matches pattern> p do matched = p
if matched != undefined do <whatever i want>

CGTalk Moderation
06-29-2011, 10:08 AM
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.