I looked at a lot of information about batch arrays, but pretty much all of it concerns iterating through the list. Is there a way to directly print a value at specific inde
If you don't want to iterate neither on retrieving a value nor on building an array,
you can use self expanding code.
But beware of getting headache when trying to understand the code.
@echo off & setlocal enabledelayedexpansion
Set i=0 & set "ITEM_LIST= item1 item2 item3 item4 item5"
Set "ITEM_LIST=%ITEM_LIST: ="&Set /a i+=1&Set "ITEM_LIST[!i!]=%"
Echo List has %i% entries
Set ITEM_LIST
echo !ITEM_LIST[1]!
echo %ITEM_LIST[5]%
Sample run:
> SO_45491838.cmd
List has 5 entries
ITEM_LIST[1]=item1
ITEM_LIST[2]=item2
ITEM_LIST[3]=item3
ITEM_LIST[4]=item4
ITEM_LIST[5]=item5
item1
item5
Just to append two other common uses:
@echo off & setlocal enabledelayedexpansion
Rem Set MonthName[01..12] to short English month names
Set i=100&Set "MonthName= Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
Set "MonthName=%MonthName: ="&Set /a i+=1&Set "MonthName[!i:~-2!]=%"
Set MonthName
Rem Set DayName[1..7] to short English day names
Set i=0&Set "DayName= Mon Tue Wed Thu Fri Sat Sun"
Set "DayName=%DayName: ="&Set /a i+=1&Set "DayName[!i!]=%"
Set DayName
You can retrieve a value from the array by using subscript syntax, passing the index of the value you want to retrieve within square brackets immediately after the name of the array.
For example :
set a[0]=1
echo %a[0]%
In you code it will be
echo %ITEM_LIST[0]%
Were you to set your list with
set ITEM_LIST=item1 item2 item3 item4 item5
then
for /f "tokens=3" %%a in ("%item_list%") do echo %%a
should show you the third item.