Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Manually incrementing the counter in a counted DO loop?

May
13,823
211
Can it be done?

Below, I was expecting and hoping for 1 3 5 7 9.

Code:
v:\> do i=1 to 10 ( echos %i^^s & set i=%@inc[%i] )
1 2 3 4 5 6 7 8 9 10

It seems DO sets the environment variable but doesn't check/use it as the counter. I know it's ancient behavior. Is there a reason for it?
 
This method gets the results that you want;
Code:
E:\Utils>do i=1 to 10 by 2 ( echos %i^^s ) 
1 3 5 7 9

Joe
 
Can it be done?

No.

Below, I was expecting and hoping for 1 3 5 7 9.

Code:
v:\> do i=1 to 10 ( echos %i^^s & set i=%@inc[%i] )
1 2 3 4 5 6 7 8 9 10

It seems DO sets the environment variable but doesn't check/use it as the counter. I know it's ancient behavior. Is there a reason for it?

DO uses the start value + the step value as the counter. The reason for it is that I had dozens of bogus "bug" reports from users who inadvertently used the same variable name for another reason in their DO loops (or even setting string values, which resulted in infinite loops).

There's lots of ways to do this, including DO with a step or a different DO test.
 
There's lots of ways to do this, including DO with a step or a different DO test.
The stepping (1 3 5 7 9) was just a simple example. I was parsing parameters (DO i=1 to %# and SWITCH). When I came across a parameter that took an argument I wanted to increment the counter, grab that argument, leave the SWITCH and continue the DO (going to the parameter after that argument).

I settled on DO WHILE "%1" NE "" and a lot of SHIFTs.

I'd be glad to hear other strategies?
 
P.S., if I use DO i=1 to %# and SHIFT, will DO re-evaluate %#?
 
Isn't that what ITERATE does?
I don't think so. If I say do i=1 to %end and change %end in the body of the loop, DO doesn't know about the change. It's the same with do i=1 to %#. SHIFT changes %# but DO doesn't know about it. I don't think ITERATE changes anything.

But I can get the 1 3 5 7 9 in a rather awkward (certainly not robust) way. This shows how and also that DO doesn't know that %# has changed.

Code:
v:\> type shifttest.btm
setlocal
do i=1 to %#
    echo %[%i]
    shift
    iterate
enddo

v:\> shifttest.btm 1 2 3 4 5 6 7 8 9
1
3
5
7
9
ECHO is OFF
ECHO is OFF
ECHO is OFF
ECHO is OFF
 
The reason for it is that I had dozens of bogus "bug" reports from users who inadvertently used the same variable name for another reason in their DO loops (or even setting string values, which resulted in infinite loops).
I'm surprised. That's a pretty dumb mistake and, once made, it's pretty easy to figure out what went wrong. Do you think that mistake would be made often today?

I wish DO would re-evaluate its counter and END-spec every time through the loop. That would make it much more useful.
 
Back
Top