How to access array elements?

May 20, 2008
3,515
4
Elkridge, MD, USA
What is the syntax by which commands in a batch file can access the elements
of an array when the name of the array is the first of the calling
parameters of the batch file?

Example:

--- file caller.btm ---
setarray myarray[1,2,3,4]
call xyz.btm myarray
--- end of file caller.btm ---

--- file xyz.btm ---
echo array %1
for /l %n in (0,1,5) echo %n %@arrayinfo[%1,%n]
switch %@arrayinfo[%1,0]
case 1
echo %1[0]
case 2
echo %1[0,0]
case 3
echo %1[0,0,0]
case 4
echo %1[0,0,0,0]
endswitch
quit
--- end of file xyz.btm ---

In the above example, I also tried %[1[0]] and %[%1[0]] in lieu of %1[0].

Note: I came across this problem when I tried to write a batch file to
perform the reverse of the function @filearray, i.e., to write the contents
of an array to a file.
--
Steve
 
  • Like
Reactions: krischik

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,357
82
Albuquerque, NM
prospero.unm.edu
What is the syntax by which commands in a batch file can access the elements
of an array when the name of the array is the first of the calling
parameters of the batch file?

Borrowing a clever trick someone posted recently:

Code:
@echo off
echo array %1
for /l %n in (0,1,5) echo %n %@arrayinfo[%1,%n]
echo.
echo Dump first element:
switch %@arrayinfo[%1,0]
case 1
echo %@execstr[set %1[0]]
case 2
echo %@execstr[set %1[0,0]]
case 3
echo %@execstr[set %1[0,0,0]]
case 4
echo %@execstr[set %1[0,0,0,0]]
endswitch
quit
 

samintz

Scott Mintz
May 20, 2008
1,465
15
Solon, OH, USA
Surprisingly, this does *not* work either:

set arr=%1[0]
echo %arr %[%arr]

-Scott


Charles Dye <> wrote on 10/21/2010 12:20:30 PM:


> ---Quote (Originally by Steve Fábián)---
> What is the syntax by which commands in a batch file can access the
elements

> of an array when the name of the array is the first of the calling
> parameters of the batch file?
> ---End Quote---
> Borrowing a clever trick someone posted recently:
>
>
> Code:
> ---------
> @echo off
> echo array %1
> for /l %n in (0,1,5) echo %n %@arrayinfo[%1,%n]
> echo.
> echo Dump first element:
> switch %@arrayinfo[%1,0]
> case 1
> echo %@execstr[set %1[0]]
> case 2
> echo %@execstr[set %1[0,0]]
> case 3
> echo %@execstr[set %1[0,0,0]]
> case 4
> echo %@execstr[set %1[0,0,0,0]]
> endswitch
> quit
> ---------
>
>
>
 
  • Angry
Reactions: krischik
Aug 9, 2009
197
1
Use ? if you know how to "set" them and "access" them, then using the data for a tasks
requires you to understand why you would use an array in the first place. The net is full or examples
on why you would use and array (database, speed, simplicity) come to mind.

IDSurnameFirst NameAddress
PhoneDOBDepartmentPosition

setarray array1[2,5]

: # set elements to an array

set array1[0,0]=007
set array1[0,1]=Bond
set array1[0,2]=James
set array1[0,3]=England
set array1[0,4]=0900 $365 a call
set array1[1,0]=Unknown
set array1[1,1]=Agent
set array1[1,2]=Womanizer

: # access elements in array
echo %array1[0,0] - %array1[0,1] - %array1[0,2] - %array1[0,3] - %array1[0,4]
echo %array1[1,0] - %array1[1,1] - %array1[1,2]
unsetarray array1