# _DO_LOOP and nested DOs?

#### vefatica

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.

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

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.

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

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

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.

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

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?

Not without rewriting DO and the variable expansion part of the parser.

But you can write a plugin to do it.

But you can write a plugin to do it.
Are you being facetious (as in write my own DO)? If you're serious, what do you have in mind?

Not being facetious -- if you want customized DO behavior, it'll be easier for you to write your own DO than for me to rewrite DO (and break backwards compatibility with everybody else).

Replies
9
Views
3K
Replies
3
Views
4K
Replies
2
Views
230
Replies
4
Views
458
Replies
10
Views
850
Replies
5
Views
859
Replies
0
Views
710
Replies
11
Views
1K
Replies
1
Views
731
Replies
5
Views
1K
Replies
4
Views
1K
Replies
2
Views
1K
Replies
5
Views
1K
Replies
3
Views
1K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
10
Views
2K
Replies
15
Views
2K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
6
Views
3K
Replies
6
Views
3K
Replies
54
Views
27K
Replies
28
Views
915
Replies
1
Views
787
Replies
2
Views
1K
Replies
12
Views
4K
Replies
2
Views
1K
Replies
13
Views
3K
Replies
9
Views
3K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
14
Views
3K
Replies
3
Views
16K
Replies
0
Views
2K
Replies
12
Views
6K
Replies
13
Views
8K
Replies
4
Views
456
Replies
22
Views
910
Replies
12
Views
2K
Replies
2
Views
1K
Replies
1
Views
3K
Replies
3
Views
2K
Replies
33
Views
4K
D
Replies
4
Views
2K
Replies
4
Views
2K
Replies
7
Views
6K
Replies
6
Views
5K