0

I'm defining lists in a batch script and then like to print a specific element in each one of them, but getting an 'ECHO is off' output (is if it's empty).

I tried cycling trough the lists with a FOR loop and that worked fine.

This is the code for i'm trying to run

@echo off

rem --------start of Define list--------
set clist= A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
set ilist= X Y Z A B C D E F G H I J K L M N O P Q R S T U V W 
set testl= 1 2 3 4
rem --------end of Define list--------

echo %clist[1]%
echo %ilist[1]%
echo %testl[1]%

Expected output:

B
Y
2

Actual output:

ECHO is off
ECHO is off
ECHO is off
Avi Dery
  • 3
  • 1
  • 1
    A space separated list does not an array make. When `cmd.exe` people talk about an "array," they usually mean creating multiple variables. `var[0]`, `var[1]`, `var[2]`, etc. – lit Oct 15 '19 at 11:56
  • There is no any concept of lists or arrays in batch scripting, only normal environment variables (like `VAR`); but those are sometimes named like array-elements (like `VAR[0]`, `VAR[1]`, etc.), which I call usually pseudo-arrays. Take a loop at this post: [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990) – aschipfl Oct 15 '19 at 14:49

3 Answers3

1

Here's an example using the method described here, for creating your array like variables:

@Echo Off & SetLocal EnableDelayedExpansion

Rem ------- Start of define list -------
Set "clist=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
Set "ilist=X Y Z A B C D E F G H I J K L M N O P Q R S T U V W" 
Set "testl=1 2 3 4"
Rem -------- End of define list --------

Rem ------- Start of array lists -------
Set "i=0"
Set "clist[!i!]=%clist: =" & Set /A i+=1 & Set "clist[!i!]=%"
Set "i=0"
Set "ilist[!i!]=%ilist: =" & Set /A i+=1 & Set "ilist[!i!]=%"
Set "i=0"
Set "testl[!i!]=%testl: ="& Set /A i+=1 & Set "testl[!i!]=%"
Set "i="
Rem -------- End of array lists --------

Rem ----- Start your commands here -----
Echo %clist[1]%
Echo %ilist[1]%
Echo %testl[1]%
Pause
Rem ------ End your commands here ------

EndLocal & GoTo :EOF
Compo
  • 36,585
  • 5
  • 27
  • 39
0

If it was really your intention mimic an array, then it would be something similar to this.

@echo off
setlocal EnableDelayedExpansion

set "clist=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
set /a cnt=0
for %%a in (%clist%) do (
   set "clist[!cnt!]=%%a"
   set /a cnt+=1
)
for /l %%i in (0,1,!cnt!) do echo( clist[%%i]=!clist[%%i]!

You can also separately echo the variables.

echo %clist[1]%`
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

If your "array" member values are always 1 character long, then a single variable with substring operations is all that is needed.

@echo off

rem --------start of Define list--------
set "clist=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "ilist=XYZABCDEFGHIJKLMNOPQRSTUVW" 
set "testl=1234"
rem --------end of Define list--------

echo %clist:~1,1%
echo %ilist:~1,1%
echo %testl:~1,1%

rem Show all values in loop
setlocal enableDelayedExpansion
for /l %%N in (0 1 25) do (
  echo clist[%%N] = !clist:~%%N,1!
  echo ilist[%%N] = !ilist:~%%N,1!
  echo testl[%%N] = !testl:~%%N,1!
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
dbenham
  • 127,446
  • 28
  • 251
  • 390