Re: Dynamic foreach algorithm



"H. S. Lahman" <hsl@xxxxxxxxxxxxxxxxx> wrote:
Responding to Daniel T...

foreach(level1 = 0; level1 < somecount; level++) {
foreach(level2 = 0; level2 < somecount2; level2++) {
// do something with level1 and level2
}
}

If you knew in advance that you three such levels you would do this

foreach(level1 = 0; level1 < somecount; level++) {
foreach(level2 = 0; level2 < somecount2; level2++) {
foreach(level3 = 0; level3 < somecount3; level3++) {
// do something using level1, level2, level3
}
}
}

First note that the bodies of these two examples are necessarily
different because they are parameterized differently (i.e., the
second example needs level3 but the first does not).

We really don't know that for sure. The bodies of the loops in the
two examples may very well not *need* the extra variable. That's why
I asked early on what they looked like.

I was taking his code comments at face value.

Even taking the comments at face value, there may not be a need for the
extra variables. For example, he may be iterating through a
multi-dimensional array using the variables, but that can be done
without them...

I just felt is was important to get the OP to look for a simpler
solution before trying to make the complex solution work. :-)
.