DO i=1 to %hi, re-evaluate hi each time

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
Could DO i=1 to %hi be made to re-evaluate hi on each iteration? ... for example, so that
Code:
do i=1 to 10 ( echos %i & set /q /a hi-=1 )
would produce
Code:
12345
I had a use for it today.
 

samintz

Scott Mintz
May 20, 2008
1,204
11
Solon, OH, USA
#4
Well, apparently, if you leave off the %'s in front of the hi the script runs just fine but the variable is not evaluated. If you add in the %'s it does what is expected.
Code:
set hi=1 & do while %hi gt 0 (echo hi & set hi=0)
 

samintz

Scott Mintz
May 20, 2008
1,204
11
Solon, OH, USA
#6
The conditional is very picky though.
This does not work:
Code:
DO WHILE %i != %hi (...)
And this code returns an error:
Code:
$ set done=0 & set hi=10 & set i=1 & do while %done==0 (echos %i & set /q /a i+=1 & set /q /a hi-=1 & set done=%@if[%i==%hi,1,0])
TCC: Unknown command "set"
 

samintz

Scott Mintz
May 20, 2008
1,204
11
Solon, OH, USA
#7
My bad. Bad copy & paste. This works, but is ugly.
Code:
set done=0 & set hi=10 & set i=1 & do while %done==0 (echos %i & set /q /a i+=1 & set /q /a hi-=1 & if %i GE %hi (set done=1) else (set done=0)
)