为什么我的批处理代码无法获取数组的最小值?

Why is my batch code to get minimum value of an array not working?

我正在尝试检索我创建的数组中的最小值,该数组是从另一个批处理文件中调用的。

数组创建正常,但 for /l 不工作。我认为,if 语句有一些东西:

@echo off

for /f "usebackq" %%a in ('%2') do set d=%%~a
for /f "usebackq tokens=* delims=%d%" %%G in ('%3')  do set %1=%%~G

set /a i=-1

for %%h in (!%1!) do (
    set /a i+=1
    set %1[!i!]=%%h
)

if %4==min (
    set /a n=%i%
    for /l %%j in (0,1,%n%) do (
        if %%j==0 (
            set %4=!%1[%%j]!
        ) else (
            if !%1[%%j]! lss !%4! (
                set %4=!%1[%%j]!
            )
        )
    ) else (
        set %4="Please write the name of the function correctly"
    )

:::: below the file im calling this function

@echo off
setlocal EnableDelayedExpansion
call test char "," "30,10,40" min

echo !min!
:: char is %1
:: "," is %2
:: "30,10,40" is %3
:: min is %4
pause

我重新整理了您的代码以对其进行测试,并修改了一些部分以修复一些细节。请参阅我在代码中以大写字母表示的注释:

@echo off
setlocal EnableDelayedExpansion
call :test char "," "30,10,40" min

echo !min!
:: char is %1
:: "," is %2
:: "30,10,40" is %3
:: min is %4
GOTO :EOF


:TEST

REM for /f "usebackq" %%a in ('%2') do set d=%%~a
REM Previous line is equivalent to this one:
set d=%~2

REM for /f "usebackq tokens=* delims=%d%" %%G in ('%3')  do set %1=%%~G
REM I don't understand what you want to do in previous line,
REM but the next two lines replace the delimiter in %d% by spaces:
set %1=%~3
set %1=!%1:%d%= !

set /a i=-1

for %%h in (!%1!) do (
    set /a i+=1
    set %1[!i!]=%%h
)

if %4==min (
    set /a n=%i%
    REM The next line requires delayed expansion in !n! variable
    REM or use %i% instead
    for /l %%j in (0,1,!n!) do (
        if %%j==0 (
            set %4=!%1[%%j]!
        ) else (
            if !%1[%%j]! lss !%4! (
                set %4=!%1[%%j]!
                REM The next right parentheses was missing
            )
        )
    )
) else (
    set %4="Please write the name of the function correctly"
)

EXIT /B