1

I want to have:

a=id
b=name
c=age

here is the batch windows file I wrote:

setlocal enabledelayedexpansion
set MY_LIST[0]=id
set MY_LIST[1]=name
set MY_LIST[2]=age
set /A COUNTER=0
FOR %%A IN (A B C) DO (

  ECHO %%A=%MY_LIST[!COUNTER!]%
  set /A COUNTER+=1
)
endlocal

why this is not working?

Matoy
  • 1,738
  • 3
  • 22
  • 53
  • 1
    What makes you think batch files _have_ array indices? (Hint: they don't!) You can sort of get something like what you're trying: drop all mentions of `COUNTER`, use `for %%a in (0 1 2)` (or the `for /l` construct) and then `echo %%a=!MY_LIST[%%a]!`. You need `!` to access the (three different) `MY_LIST` variables because you're constructing the name each time around the loop. – TripeHound Jun 23 '16 at 12:46
  • 2
    @TripeHound *"if a beginner would ask: "How to write a message in C language?", do you think the right answer should be: 'You can't. C language does not formally support any I/O statement, but you can emulate such operations via library functions'? Of course not!... Well, in my modest opinion, the same approach should be used in the case of arrays in Batch."* -- [Antonio Acini](http://stackoverflow.com/a/10569981/1683264); It's not productive to claim that the Batch language doesn't support arrays unless the limitations are directly relevant (i.e. an attempt to call an array's .length method). – rojo Jun 23 '16 at 13:07
  • I was trying to point out -- probably more tongue-in-cheek/sarcastically than I should (apologies to OP if any offence caused) -- that these _aren't_ arrays -- they are just multiple environment variables whose names just happen to have square brackets in them. I hadn't seen the Q/A you quoted from (nor realised that people informally thought of "arrays in batch files") ... Having read it, I _personally_ would tend to side with [dbenham](http://stackoverflow.com/a/10545035/2096401) in that using an array-like name brings too much baggage / expectations. – TripeHound Jun 23 '16 at 14:40
  • 1
    @TripeHound It also improves readability, clearly communicating the author's intended use of the collection of variables. The variables are obviously a list of related values, assigned properties of a single list object (functionally speaking). OP is well aware that he can't call methods often found in other languages on this list. He obviously didn't ask why he couldn't `MY_LIST.push("new value")`. His code is well-organized and not deserving of criticism about how "these *aren't* arrays" which offers no path to solving his problem. – rojo Jun 23 '16 at 15:42

1 Answers1

4

You've got your order of delayed expansion inverted. Where you have %MY_LIST[!COUNTER!]%, the interpreter hasn't expanded the inner portion at the time it attempts to expand the outer. You need to find another way to expand !COUNTER! before using it as your array index, and to delay expansion of !MY_LIST[n]! even longer than that of !COUNTER!. Here's one way:

for %%A in (A B C) do (

    for %%I in ("!COUNTER!") do ECHO %%A=!MY_LIST[%%~I]!
    set /A COUNTER += 1
)
rojo
  • 24,000
  • 5
  • 55
  • 101
  • thanks.. it works. one question - why this happened? why " the interpreter hasn't expanded the inner portion at the time it attempts to expand the outer" in my code? – Matoy Jun 23 '16 at 12:51
  • 1
    @Matoy Because percent variables are expanded before the loop is run. At the time this percent variable is expanded, !COUNTER! doesn't have a value yet. %COUNTER% does, but !COUNTER! hasn't been expanded yet. It's basically a string literal "COUNTER" surrounded by exclamation marks at that point. – rojo Jun 23 '16 at 13:02