How to access array elements?

#1
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
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,575
46
Albuquerque, NM
prospero.unm.edu
#2
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,271
11
Solon, OH, USA
#3
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
> ---------
>
>
>