Variable number of loop nesting
-
Strange question, but I'm sure someone's dealt with it. When there are say 2 dimensions, and we want to loop within each of the 2, we write for a = 0 to x for b = 0 to x do something... next next And if there are 3 dimensions, we just add a c loop within the b loop, etc. What if we don't know ahead of time how many dimensions there are? Can we write the looping structure to be dynamic based on a specified number of required loops? Thanks
-
Strange question, but I'm sure someone's dealt with it. When there are say 2 dimensions, and we want to loop within each of the 2, we write for a = 0 to x for b = 0 to x do something... next next And if there are 3 dimensions, we just add a c loop within the b loop, etc. What if we don't know ahead of time how many dimensions there are? Can we write the looping structure to be dynamic based on a specified number of required loops? Thanks
depending on circumstances recursion, reflection or a simple
foreach
may do it. :)Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Strange question, but I'm sure someone's dealt with it. When there are say 2 dimensions, and we want to loop within each of the 2, we write for a = 0 to x for b = 0 to x do something... next next And if there are 3 dimensions, we just add a c loop within the b loop, etc. What if we don't know ahead of time how many dimensions there are? Can we write the looping structure to be dynamic based on a specified number of required loops? Thanks
That would be awful. Unless you just cannot avoid it, don't use it. What if there are 10 nested loops each going from 0 to 1000? If there will be no code between the two loops, you can do something like this:
for(int i = 0; i
Then in the Looper method:private void Looper(loopFrom,loopTill){
for(int i=loopFrom;iLooks awful.
You can also generate dynamic code blocks and inject when required.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
That would be awful. Unless you just cannot avoid it, don't use it. What if there are 10 nested loops each going from 0 to 1000? If there will be no code between the two loops, you can do something like this:
for(int i = 0; i
Then in the Looper method:private void Looper(loopFrom,loopTill){
for(int i=loopFrom;iLooks awful.
You can also generate dynamic code blocks and inject when required.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
Thanks for the ideas. I figured out a way to do this with a collection. Roughly, you make your own looping code: Make a collection with the same number of rows as you have dimensions Each row has a property indicating its number of iterations Make a method getvalue in the collection When you call getvalue, if it's the last row in the collection (innermost loop which is incremented on every call) then increment the index by 1 if we're at the highest value of the dimension, then if we're not in the first row of the collection then return to zero increment the value of the index in the prior row (return to zero if necessary) Return the value Works like a charm
-
Thanks for the ideas. I figured out a way to do this with a collection. Roughly, you make your own looping code: Make a collection with the same number of rows as you have dimensions Each row has a property indicating its number of iterations Make a method getvalue in the collection When you call getvalue, if it's the last row in the collection (innermost loop which is incremented on every call) then increment the index by 1 if we're at the highest value of the dimension, then if we're not in the first row of the collection then return to zero increment the value of the index in the prior row (return to zero if necessary) Return the value Works like a charm