0
@echo off
set myArray[0]=a1
set myArray[1]=b2
set myArray[2]=c3
set myArray[3]=d4
set myArray[4]=e5
set myArray[5]=f6
set myArray[6]=g7
set myArray[7]=h8
set myArray[8]=i8
set myArray[9]=j9

set /a count=1
for /F "tokens=2 delims==" %%s in ('set myArray[') Do (
    echo %count% %%s
    set /a count=%count%+1
)
pause

If I have a set of array is being used in the batch file as shown above, may I know what is the simplest method to print them out to have a output like below? What kind of for loop should I use?

1 a1
1 b2
1 c3
1 d4
1 e5
1 f6
1 g7
1 h8
1 i8
1 j9
Pi-Turn
  • 137
  • 4
  • 17
  • possible duplicate of [How to loop through array in batch?](http://stackoverflow.com/questions/18462169/how-to-loop-through-array-in-batch) – Giacomo1968 Jun 11 '14 at 03:04

2 Answers2

1

for the expanded question:

use delayed expansion. Enable it with setlocal enabledelayedexpansion at the begining of the script. Whenever you change a variable in a block (everything between (and )) and use it inside the same block, you have to use !instead of %.

@echo off
setlocal enabledelayedexpansion
...
set myArray[9]=j9

set /a count=1
for /F "tokens=2 delims==" %%s in ('set myArray[') Do (
    echo !count! %%s
    set /a count+=1
)
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • one more observation from me is when I use the above for loop to print out the array content, actually it will not follow the index ascending order when you have more than 10 indexes used...For example, if you have 13 indexes, then it will follow this order which is 0,10,11,12,1,2,3... instead of 0,1,2,3,4,5... is it because it treat the index as string? – Pi-Turn Jun 11 '14 at 05:36
  • @Pi-Turn: From [this answer](http://stackoverflow.com/questions/24103920/delete-and-replace-variables/24105936#24105936): "Note that variables are sorted alphabetically, so if more than 9 variables exists, and you want to preserve its "natural order", variables less than 10 must have a left zero: var01=Hello". See: http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990 – Aacini Jun 11 '14 at 05:48
  • @Aacini, what if I don't want to have left zero? Is there still other workaround? – Pi-Turn Jun 11 '14 at 06:29
  • @Pi-Turn: Then you must keep a variable with the number of the last element: `for /L %%i in (0,1,%lastElem%) do echo !myArray[%%i]!` – Aacini Jun 11 '14 at 14:01
0
for /l %%i in (0,1,9) do echo(%myarray[%%i]%

should do the trick - 'though you omitted i8 from your list.

Question is - is this really what you want to do - given that your assignment-order happens to be alphabetical?

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • sorry for the typo...i already correct it.. I think your code is not works in my case but i tried `for /F "tokens=2 delims==" %%s in ('set myArray[') do echo %%s` and it is works for me... – Pi-Turn Jun 11 '14 at 03:19
  • One more question, if I don't want to use for loop but just want to print out the content of certain index, may I know how should I code?...the silly things I tried is 'echo %myArray%[5]' and obviously it did not work... – Pi-Turn Jun 11 '14 at 03:21
  • @Pi-Turn: `echo %myArray[5]%`. The `[5]` is part of the variablename. – Stephan Jun 11 '14 at 04:57
  • @Stephan, Thank You. Now I further expand my code as shown in the modified question above...Array can be printed out successfully, but just the count never increase while the array printed out line by line...I double check few times and I don't spot anything that I code wrong... – Pi-Turn Jun 11 '14 at 05:06
  • @Stephan, Sorry to let you know i run into error again... What if both my array also is a variable, then how should code it? I tried like this `echo %myArray[!index!]%` but it does not work...Hmm..originally my index is already an expansion variable... – Pi-Turn Jun 11 '14 at 09:10
  • @Pi-Turn: use this way: `echo !myArray[%index%]!` – Stephan Jun 11 '14 at 13:34