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,469
88
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,503
16
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
230
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
 
Similar threads
Thread starter Title Forum Replies Date
Jesse Heines Access Denied errors in TCC 27 Support 9
Jay Sage Directory Alias Access to FTP Servers Support 2
vefatica "Access denied" messages from DO Support 3
O TPIPE: Error -Can´t access textpipeengine.dll Support 24
C How to? Setting up SMTP access for Yahoo with TCMD v20 Support 7
dcantor Unable to access help Support 9
P Access denied TakeCommand.19.0.key Support 2
D How to? How to access iPhone from tcmd.exe? Support 1
vefatica TPIPE access violations are back Support 2
vefatica TPIPE access violation again Support 1
vefatica TPIPE: Access violation Support 5
S WAD Cannot access mapped drive in 64b Win7 Support 15
C registration proboem if no internet access given Support 0
cgunhouse TPIPE: Error - Can't access textpipeengine.dll Support 14
P Can't access textpipeengine.dll Support 11
Q TCC: (Sys) The process cannot access the file because it is being used by another process. Support 0
C Cannot register v12.10 without Internet access Support 19
vefatica Random access to the characters in a string? Support 4
S FTP site; IFTP access Support 10
Roedy tccbatch Windows 7 "access is denied" Support 4
T Access violation upon TC startup Support 2
M Testing my e-mail access Support 1
mscheuner How to access XML *attributes* using XPath Support 3
p.f.moore Broken email access to these forums - finally giving up Support 4
K_Meinhard Email access, again Support 2
K_Meinhard email access Support 6
rconn Email access Support 6
C access denied problem under Vista Support 5
D Parsing an array variable to an array does not work Support 5
vefatica Setting array variable ignores leading spaces (and @execarray doesn't) Support 4
vefatica SET /A and multidimensional array. Support 1
vefatica Array name in a variable? Support 3
J WAD "setarray /f" only overwrites the first array var Support 2
J How to? Array sort according to multiple keys Support 3
S Documentation "DEFINED" vs. array elements Support 1
Frank watch an array variable in ide/bdebugger Support 2
S WAD SET problem - array element initialization Support 11
C Simple .bat question - dump contents of 2 dimensional array Support 2
Charles Dye Array limits Support 8
S ARRAY enhancements Support 0
vefatica Leading spaces in array variables Support 13
A What is the fastest way to dump a array Support 4

Similar threads