简体   繁体   English

批处理:如何在 for 循环中使用带有变量的数组作为索引?

[英]Batch: How to use an array with a variable as an index inside a for loop?

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.我的代码让客户输入一系列单词(直到客户输入“0”),将它们存储在一个数组中,然后按字母顺序对该数组进行排序。 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?我找到了一种方法,可以通过使用额外的 for 循环来使我的代码正常工作,但我对这种方法不满意,请问您还有其他替代方法吗? (the one with %%h) (带有 %%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.我的问题来自%%tab[!next!]%%并且我需要再延迟一次,当我使用CALL echo时它有效但我不知道如何处理 if 条件和集合。

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. ps:对从 1 开始的数组和 for 循环感到抱歉,但这让我又跳过了 1 个 goto 和计算循环索引+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

The more efficient way to expand delayed variables for use as in index within a code block is with a simple for loop:扩展延迟变量以在代码块中用作索引的更有效方法是使用简单for循环:

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 /f循环来扩展和隔离所需的标记。

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
    )
)

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.将剩余条目中最低的放入索引%%v中,直到完成。

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.请注意,需要在引号中执行字符串比较,以迎合包含空格的条目,并为字符串分配set "var=value" ,以确保分配的值中不包含尾随空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM