Home > Back-end >  Batch: How to use an array with a variable as an index inside a for loop?
Batch: How to use an array with a variable as an index inside a for loop?

Time:03-16

My code offers a client to enter a sequence of words (until the client enters "0"), stores them in an array and then sort that array alphabetically. I have found a way to make my code work by using an extra for loop but I'm not happy with that way to do, do you have any alternative to that 3rd for, please? (the one with %%h)

My issue was coming from %%tab[!next!]%% and my need to delay one more time, it works when I use CALL echo but then I don't know how to do with the if conditions and the set.

Thank you in advance.

ps: sorry about the array and for loops starting at 1 but it makes me skip 1 more goto and the struggle to calculate the loop indexes 1.

@echo off
setlocal enableDelayedExpansion

set /a num=0

:newVar
set /a num =1
set /p tab[%num%]=Write something: 
if not !tab[%num%]! == 0 goto newVar
set /a num-=1

echo.
echo Array length: %num%
echo.

set varTemp=ERROR
set /a nbTours=%num%-1

FOR /L %%v IN (1 1 %nbTours%) DO (
   echo ***********************************************
   echo.
   set /a nbComparaison=%num%-%%v
   set /a next=1

   FOR /L %%i IN (1 1 !nbComparaison!) DO  (
      set /a next=next 1
      call echo tab[%%i] = !tab[%%i]! . tab[!next!] = %%tab[!next!]%%
      FOR %%h in (!next!) do (
         if !tab[%%i]! GTR !tab[%%h]! (
         echo ________ !tab[%%i]! ^<-^> !tab[%%h]! & echo. 
         set varTemp=!tab[%%h]!
         set tab[%%h]=!tab[%%i]!
         set tab[%%i]=!varTemp!
         )
      )
   )
   echo.
)
echo ***********************************************

echo.
echo result:
FOR /L %%d IN (1 1 %num%) DO  (
   echo tab[%%d] = !tab[%%d]!
)

endlocal
pause

CodePudding user response:

The more efficient way to expand delayed variables for use as in index within a code block is with a simple for loop:

For %%G in (!next!)Do echo(tab[%%i] = !tab[%%i]! . tab[%%G] = !tab[%%G]!

If multiple delayed variables are needed within a code block as index variables, a for /f loop can be used to expand and isolate the required tokens.

For /l %%i in (1 1 10) Do (
    Set /A Next=%%i 1,Previous=%%i-1
    For /f "tokens=1,2,3 delims=;" %%G in ("!Pevious!;!Next!;%%i") Do (
        Echo(%%G - %%H - %%I
    )
)

CodePudding user response:

FOR /L %%v IN (1,1,%num%) DO IF (%%v neq %num%) FOR /L %%i IN (%%v,1,%num%) DO IF (%%v neq %%i) IF "!tab[%%v]!" gtr "!tab[%%i]!" (
 SET "vartemp=!tab[%%v]!"
 SET "tab[%%v]=!tab[%%i]!"
 SET "tab[%%i]=!vartemp!"
)

for each entry, except the last, for each remaining entry (those with a higher index) do the comparison and swap if required.

Puts the lowest of the remaining entries in index %%v until done.

Note that string comparisons need to be performed in quotes to cater for entries containing spaces and preferred syntax set "var=value" for string assignments to ensure trailing spaces are not included in the value assigned.

  • Related