_DO_LOOP and nested DOs?

#1
Does each DO loop have its own _DO_LOOP? If so, I'd expect this code
Code:
do i=1 to 2
   echos %_do_loop^s
   do j=1 to 2
     echos %_do_loop^s
     do k=1 to 2
       echos %_do_loop^s
     enddo
   enddo
enddo
to produce
Code:
1 1 1 2 2 1 2 2 1 1 2 2 1 2
If there's only one _DO_LOOP, I'd expect
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
But I get
Code:
1 1 1 2 4 1 2 5 1 1 2 4 1 2
which makes no sense to me.
 

samintz

Scott Mintz
May 20, 2008
1,295
11
Solon, OH, USA
#2
It sorta kinda seems like the outer loop counters include the inner loop counts. However, that's not correct either.
Code:
do i=1 to 2
   echo i=%i %_do_loop
   do j=1 to 2
     echo j=%j %_do_loop
     do k=1 to 2
       echo k=%k %_do_loop
     enddo
   enddo
enddo
produces this output
Code:
i=1 1
j=1 1
k=1 1
k=2 2
j=2 4
k=1 1
k=2 2
i=2 5
j=1 1
k=1 1
k=2 2
j=2 4
k=1 1
k=2 2
 

samintz

Scott Mintz
May 20, 2008
1,295
11
Solon, OH, USA
#3
The innermost loop consistently produces 1 2. Second loop consistently produces 1 (1 2) 4 (1 2). And top loop 1 (inner) 5 (inner).

If you change the top loop to 1 to 3, you get 1 (inner) 5 (inner) 5 (inner). The 5 (inner) just repeats for 3 or more.
 
#4
Scott, does it make sense to you? I was off on my expectation of what a single _DO_LOOP would produce because I didn't consider that it's reset each time a DO starts. I can make some sense of the output in my original example.

Outer loop (just starting) 1
Middle loop (just starting) 1
Inner loop (just starting) 1
Inner loop 2 (_do_loop incremented to 3, loop done)
Middle loop (not done, increments _do_loop) 4
Inner loop (just starting) 1
Inner loop 2 (_do_loop incremented to 3, loop done)
Middle loop (increments _do_loop to 4, loop done)
Outer loop (not done, increments _do_loop) 5
Middle loop (just starting) 1
and so on

That gave me a headache.

So I guess there's only one _DO_LOOP. I wish each DO had its own.
 

rconn

Administrator
Staff member
May 14, 2008
10,597
97
#5
You're inventing a new & wholly imaginary definition for %_do_loop.

First, there is only one (global) _do_loop variable. Second, it is reset to 0 every time a DO statement is encountered. Third, it is incremented each time through the DO loop; it has nothing to do with nested DO's.

If you want a count of your nested DO's, you're going to have to create it yourself. It will be a trivial exercise.
 
#6
I don't want a count of the nesting. I want _DO_LOOP to tell me how many times the DO loop in which it's called has been executed. What's imaginary about that? Can't each do have its own _DO_LOOP? In nested counted loops, the counter variables are distinct, even if the same variable name is used in all the loops! Below, the value of i depends on which DO loop it's evaluated in.
Code:
do i=1 to 2
   echo outer  %i
   do i=11 to 12
     echo middle %i
     do i=101 to 102
       echo inner  %i
     enddo
   enddo
enddo
Code:
outer  1
middle 11
inner  101
inner  102
middle 12
inner  101
inner  102
outer  2
middle 11
inner  101
inner  102
middle 12
inner  101
inner  102