Batch script cannot find lowest value

Viewed 31

When i input something it only outputs the last value and not the lowest

echo.
echo enter number
set temp=9999
set /p pirm=

        if "%pirm%" equ "" goto error

(for %%a in (%pirm%) do (

    if %temp% gtr %%a (
    set/a temp=%%a
    echo temp = %temp% , a = %%a
    pause>nul
    ) else (
    echo else
    )
))
echo Lowest number
echo %temp%

goto reset2

INPUT:

1 5 7 9 3 25 15

OUTPUT:

enter number
1 5 7 9 3 25 15
temp = 100 , a = 1
temp = 100 , a = 5
temp = 100 , a = 7
temp = 100 , a = 9
temp = 100 , a = 3
temp = 100 , a = 25
temp = 100 , a = 15
Lowest number
15

I am pretty new with batch script so i might make a simple syntax mistake

basically what I want to do is to take my input as an array, then check if the number is lower than the current minimum, if it is make it the new minimum

1 Answers

Try with delayed expansion

setlocal enableDelayedExpansion
echo.
echo enter number
set temp=9999
set /p pirm=

if "%pirm%" equ "" goto error

(for %%a in (%pirm%) do (

    if !temp! gtr %%a (
      set/a temp=%%a
      echo temp = !temp! , a = %%a
      pause>nul
    ) else (
      echo else
    )
))
echo Lowest number
echo %temp%
Related