2015-07-19 22 views
0

我試圖從我創建的另一個批處理文件調用此數組中檢索最小值。爲什麼我的批處理代碼獲取數組的最小值不工作?

數組創建正常,但for /l不起作用。我認爲,有一些與如果聲明:

@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 
+1

你的代碼是幾乎無法讀取沒有上下文(哪個參數意味着什麼)。但至少你已經正確識別出你的問題在哪裏:'for/l'在'if'block中沒有正確運行,因爲'%n%'沒有被定義。請參閱[延遲擴展陷阱](http://stackoverflow.com/a/30284028/2152082)爲什麼它現在不是'set/an =%i%' – Stephan

+0

現在我編輯了我的帖子後可以看到輸入參數。 –

回答

0

我重新安排你的代碼,以測試和修改了一些零件修復了幾個細節。看到我的意見在大寫字母的代碼:

@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 
+0

非常感謝 –

相關問題