_DO_LOOP and nested DOs?

May 20, 2008
11,400
99
Syracuse, NY, USA
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,503
16
Solon, OH, USA
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,503
16
Solon, OH, USA
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.
 
May 20, 2008
11,400
99
Syracuse, NY, USA
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
12,344
149
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.
 
May 20, 2008
11,400
99
Syracuse, NY, USA
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
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
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.
 
Similar threads
Thread starter Title Forum Replies Date
C FOR loop with nested IF / IFF Support 9
R nested for loop question Support 3
samintz How to? Abort on error DO loop Support 10
D How to use multi-line DO to loop through first level directory names Support 5
vefatica ON ERROR terminates DO loop. Support 0
Dick Johnson for ... do loop "wrap around" Support 11
vefatica Modifying a counted DO loop's counter? Support 1
Z [dos] into - for loop - the bp is not hit Support 5
vefatica Piping the output of a DO loop. Support 4
rps For loop internal %_for_files variable? Support 2
D What is the limit on the compound statement length within ( ) of a FOR-loop? Support 5
old coot Proper use of ON ERRORMSG inside a DO WHILE loop? Support 3
H EndLocal in DO Loop Support 2
B How to? redirect a bunch of loop Support 4
Mike Hall Unexpected "Variable loop" in IF condition Support 10
D Strange issue with FOR loop Support 15
nickles WAD Variable name gets changed within FOR loop Support 2
M tcmd crashes in this recursive "for /f" loop Support 4
G Fixed Slow FOR loop Support 2
S WAD %_do_errors does not report errors when loop control variable type is not directory entry Support 1
mikea 'Loop' on closing TC Support 6
Roedy Loop anomaly Support 6
P Need to use a regex in a "for" loop. Support 54
B Problem with color in nested shells Support 1
R Problem with %_do_loop in nested do loops Support 2
C Nested IF syntax — TCC behaving differently to CMD Support 12
jbanaszczyk Broken For and nested Do Support 2
vefatica WAD Nested variable expansion in PDIR's @function[*]? Support 13
vefatica Single-line DO inside nested multi-line DOs? Support 9
R How to? debug a nested batch file Support 2
G WAD Nested if exits enclosing if Support 3
M A problem with nested Gosub's/ExecStr's Support 14
C CMD's "nested quote" syntax Support 3
dcantor Terminology question: nested vs. recursive Support 0
J Nested for loops and quotes Support 12
D can PDIR list full filepaths of docs in a nested folder? Support 13
H DOS CMD Sort+Add Sequence+Remove Sequence number Support 12
T Regex engine doesn't recognize native DOS line endings Support 2
A CDD and DOS filenames Support 1
Joe Caverly How to? Run DOS .exe's the same way Wine does... Support 3
T Associating BTM files with IDE results in DOS box Support 33
D tcmd 12 and DOS Support 4
R DOS programs Support 4
M Ansi.sys and old Dos programs Support 7
B TCC 11 & Windows 7 64bit & DOS apps? Support 6
M Invoking a DOS command within a C++ program Support 1
epement Coerce DOS programs to handle LFN Support 0

Similar threads