Problem with batch "set" command

0

Good afternoon. I was doing a very simple programming, in general, simply serves to put the numbers that the user put (10 numbers) in ascending order, however something strange happens already in that part

set /p NM9=
set /p NM10=

:CICLO
if %NM10% EQU %NM9% (
    goto IGUAL1
)
if %NM10% LEQ %NM9% (
    set /a VAR=%NM9%
    set /a NM9=%NM10%
    set /a NM10=%VAR%
)

After an echo at the beginning of the cycle, it says that the values NM9 and NM10 are "1". Can you find the problem? I suspect it's the "VAR" part, or maybe some problem in assembling the lines. Thank you.

    
asked by anonymous 08.05.2018 / 22:18

2 answers

0
  

The problem is that you are trying to do value assignment and sum   within an argument of a command that uses parentheses, using percentages in the variables, and thus is called only variables outside the IF condition and FOR p>      

How to solve this?

     

use this command at the beginning of the code SETLOCAL ENABLEDELAYEDEXPANSION

     

and change the percentages of the variables you are calling to points   that is, a variable %VAR% will look like this: !VAR!

     

Make changes, test the code and comment here   Right, blz?

    
13.05.2018 / 04:05
0
@echo off
cls
set /a VAR=0
set /p NM1=
set /p NM2=
set /p NM3=
set /p NM4=
set /p NM5=
set /p NM6=
set /p NM7=
set /p NM8=
set /p NM9=
set /p NM10=

:CICLO
if %NM10% EQU %NM9% (
    goto IGUAL1
)
if %NM10% LEQ %NM9% (
    set /a VAR=%NM9%
    set /a NM9=%NM10%
    set /a NM10=%VAR%
    goto CICLO 
)

:IGUAL1
if %NM9% EQU %NM8% (
    goto IGUAL2
)
if %NM9% LEQ %NM8% (
    set /a VAR=%NM8%
    set /a NM8=%NM9%
    set /a NM9=%VAR%
    goto CICLO 
)

:IGUAL2
if %NM8% EQU %NM7% (
    goto IGUAL3
)
if %NM8% LEQ %NM7% (
    set /a VAR=%NM7%
    set /a NM7=%NM8%
    set /a NM8=%VAR%
    goto CICLO 
)

:IGUAL3
if %NM7% EQU %NM6% (
    goto IGUAL4
)
if %NM7% LEQ %NM6% (
    set /a VAR=%NM6%
    set /a NM6=%NM7%
    set /a NM7=%VAR%
    goto CICLO 
)

:IGUAL4
if %NM6% EQU %NM5% (
    goto IGUAL5
)
if %NM6% LEQ %NM5% (
    set /a VAR=%NM5%
    set /a NM5=%NM6%
    set /a NM6=%VAR%
    goto CICLO 
)

:IGUAL5
if %NM5% EQU %NM4% (
    goto IGUAL6
)
if %NM5% LEQ %NM4% (
    set /a VAR=%NM4%
    set /a NM4=%NM5%
    set /a NM5=%VAR%
    goto CICLO 
)

:IGUAL6
if %NM4% EQU %NM3% (
    goto IGUAL7
)
if %NM4% LEQ %NM3% (
    set /a VAR=%NM3%
    set /a NM3=%NM4%
    set /a NM4=%VAR%
    goto CICLO 
)

:IGUAL7
if %NM3% EQU %NM2% (
    goto IGUAL8
)
if %NM3% LEQ %NM2% (
    set /a VAR=%NM2%
    set /a NM2=%NM3%
    set /a NM3=%VAR%
    goto CICLO 
)

:IGUAL8
if %NM2% EQU %NM1% (
    goto IGUAL9
)
if %NM2% LEQ %NM1% (
    set /a VAR=%NM1%
    set /a NM1=%NM2%
    set /a NM2=%VAR%
    goto CICLO 
)

:IGUAL9
cls
echo %NM1% %NM2% %NM3% %NM4% %NM5% %NM6% %NM7% %NM8% %NM9% %NM10%
pause

The whole code.

    
10.05.2018 / 13:52