1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'Suggestions' started by vefatica, Sep 1, 2017.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,939
    Likes Received:
    30
    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.
     
  2. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,187
    Likes Received:
    11
    How do the conditionals work in DO? I tried a simple test that doesn't work.
    Code:
    set hi=10
    do while hi gt 0 (echos %i & set /q /a hi-=1)
    
     
  3. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,187
    Likes Received:
    11
    I also tried it in a batch file and that doesn't work either.
    Code:
    set hi=1
    DO while hi gt 0
        echos 1
        set hi=0
    ENDDO
    
     
  4. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,187
    Likes Received:
    11
    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)
    
     
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,301
    Likes Received:
    39
    I imagine DO WHILE or DO UNTIL would do what you want. You'd have to handle initialization and incrementation yourself, though.
     
  6. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,187
    Likes Received:
    11
    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"
    
     
  7. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,187
    Likes Received:
    11
    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)
    )
    
     

Share This Page