Re: Dynamic foreach algorithm



In article <pdalj.5720$Wr4.2353@trnddc04>,
"H. S. Lahman" <hsl@xxxxxxxxxxxxxxxxx> wrote:

Responding to Veloz...

How would you design a solution for the problem of effectively needing
a number of nested "foreach" statements but you don't know the number
at compile that?

That is let's say your algorithm needs to produce combinations. If you
knew in advance you had two "levels" of things, you would write

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.
.