0

I'd like to start this off by saying batch scripting is something I ever do, and it's for an assignment in my class, so please bear with me. I am trying to take an array index and swap one of the indices for another. If I echo out each index after assigning it I get the expected output, but if I try to echo the array, the array hasn't changed. As I said I am very new with programming and especially batch, so I'm sure there is something fundamental I am missing.

my output

if index[0] is GTR index[4] if I enter 5,4,3,2,1:
echo %index[4]% outputs 5 %index[0] outputs 1
echo %numbers% outputs 5,4,3,2,1

my code

@echo off

set /p num1=Enter first num
set /p num2=Enter second num
set /p num3=Enter third num
set /p num4=Enter foruth num
set /p num5=Enter fifth num

SET /a num1=%num1%
SET /a num2=%num2%
SET /a num3=%num3%
SET /a num4=%num4%
SET /a num5=%num5%

SET numbers=%num1% %num2% %num3% %num4% %num5%

(for %%x in (%numbers%) do (echo %%x))

echo my array %numbers%

if %num1% GTR %num5% (
    SET /A temp=%num1%
    SET numbers[0]=%num5%
    SET numbers[4]=%temp%
    echo INDEX 4 is: %numbers[4]% INDEX 1 is: %numbers[0]%
    ) else (
        echo "end of array is greater than start"
)
avery_larry
  • 2,069
  • 1
  • 5
  • 17
  • There is no concept of arrays or lists in batch scripting; a variable `numbers[1]` is just a specifically named normal variable like `num1`; that's why I call such variables pseudo-arrays. So `numbers[1]` has got no relation to `numbers` after all... – aschipfl Nov 22 '19 at 01:02
  • I had figured that, How can I access the numbers array and replace whats needed? Hopefully I don't have to assign each one individually and then loop them – Colton Van Bastelaere Nov 22 '19 at 01:08
  • 1
    Well, you should take a look at [this post](https://stackoverflow.com/a/10167990) then... – aschipfl Nov 22 '19 at 01:14
  • 2
    You need delayedexpansion to use variables that are defined or changed inside the `if` statement. At the beginning of your code add `setlocal enabledelayedexpansion`. Inside your `if` you need to change the lines `SET numbers[4]=!temp!` and `echo INDEX 4 is: !numbers[4]! INDEX 1 is: !numbers[0]!` – avery_larry Nov 22 '19 at 03:26
  • Side note -- your code posted here cannot produce the output you have posted. – avery_larry Nov 22 '19 at 03:28
  • Side note #2 -- the `temp` variable appears to be superfluous. – avery_larry Nov 22 '19 at 03:33
  • Thank you very much for the insightful post. I did realize that my temp wasn't set /a which was causing some issues too. I have screen shots of the output so though it may not be exact that is what it was putting out, barring some possible typos in the above code. I'll post the functioning code as an answer. – Colton Van Bastelaere Nov 22 '19 at 04:31

1 Answers1

0

As in an above comment I needed to use setlocal enabledelayedexpansion after doing this I found it easier to assign each index separately, and I did not test if would work the way I had the code in my question. ie set /a value[1] = value[1]

@echo off
setlocal enabledelayedexpansion

SET /p elem[0]=Enter a num
SET /p elem[1]=Enter a num
SET /p elem[2]=Enter a num
SET /p elem[3]=Enter a num
SET /p elem[4]=Enter a num
rem CHANGE TO INTEGER
SET /a elem[0]=elem[0]
SET /a elem[1]=elem[1]
SET /a elem[2]=elem[2]
SET /a elem[3]=elem[3]
SET /a elem[4]=elem[4]
rem loop through each value 0-4, echo each index value
for /l %%n in (0,1,4) do (
    echo !elem[%%n]!
)
rem Check if value of elem[0] greater than elem[4], if so swap their positions
if %elem[0]% GTR %elem[4]% (
    set /a temp = elem[0]
    set /a elem[0] = elem[4]
    set /a elem[4] = temp

    echo Element 0 is greater than element 4 the new array is: 
    for /l %%n in (0,1,4) do (
        echo !elem[%%n]!
    ) 
rem If not greater, no swap needed
) else (echo Element 0 is NOT greater than element 4 the array is still the same)