0

I have an array that contains all of my GOTO tag names. I want to be able to use a variable as the array index and use that to jump to the specific part of my code. My code will prompt the user for an input and based on the screen they are on (%room%) and the input they give it will will jump to a specific point in my code.

I am fairly new to batch and most of my code is modified from the internet. in the folder that has this file I have another folder called 'Arrays'. In that folder I have a bunch of text files that contain my Array elements. My arrays are populated using these text files and the name of the file becomes the name of the array.

Here is my code

@Echo Off
set /A room=1

::--part a (populating arrays) ----------------------
set "file=%~dp0Arrays"

For /R %file% %%G IN (*.txt) DO (
    set /A i=0
    for /F "usebackq delims=" %%a in ("%%G") do (
    set /A i+=1
    call set %%~nG[%%i%%]=%%a
    call set n=%%i%%
    )
)

::--part b (populating arrays) ----------------------
echo %%ajump[%room%]%%
:: returns %roomtext[1]%

call echo|set /p=%%ajump[%room%]%%
:: returns dha

echo.

::--part c (jumping to point in  code using array)-----


SET /P choice= Type your choice: 
if /I %choice%==a (goto %%ajump[%room%]%%)

exit

::--part d (code to jump to) ----------------------
:dha
 ehco succsess
 pause

 exit

I populate my arrays using part a

I then use part c to jump to the specific part in the code.

However when I use run this I get "The system cannot find the batch label specified - %ajump[1]%" (omit the quotes)

and if I change the jump code to (remove a set of %)

    if /I %choice%==a (goto %ajump[%room%]%)

I get "The system cannot find the batch label specified - room" (omit the quotes)

ajump[1] reads dha

Part b concerns me because when I tire to just echo it, it echos the same value that it tries to jump to. however when I use the call set|echo /p= it echos the actual contents of the array.

I can echo all of the contents for all of the arrays in the folder 'Arrays' using this code

For /R %file% %%F IN (*.txt) DO (
    echo %%%%~nF
    for /L %%i in (1,1,10) do call echo %%%%~nF[%%i]%%
    echo.
)

Is my if statement wrong or could it be my jump label. Since it echos the same text as where it tries to jump I am lead to believe that it is the way I am calling the arrays that is the problem.

  • Not a solution, but you should replace `call echo|set /p=%%ajump[%room%]%%` by `< nul call set /P =%%ajump[%room%]%%` for the sake of performance... – aschipfl Jun 03 '19 at 06:09
  • Take a look at [this great post](https://stackoverflow.com/a/10167990) concerning arrays and lists in batch-files... – aschipfl Jun 03 '19 at 11:10

1 Answers1

0

I have found the answer. I need to include a call before my GOTO, although I don't know why.

  • 5
    it's because you need another layer of parsing, and `call` delivers that. But you should better use [delayed expanison](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) (`goto !ajump[%room%]!` together with an initializing `setlocal enabledelayedexpansion`) – Stephan Jun 02 '19 at 20:39
  • 1
    Without `call`, the `goto` command sees `%ajump[1]%` (given that `%room%` is `1`); `call` resolves the variable `%ajump[1]%` then... – aschipfl Jun 03 '19 at 06:12