0

I have a problem witch .bat file I have somethis like this:

@echo off
setlocal EnableDelayedExpansion

set arr[a]=1
set arr[b]=2

for /F "tokens=*" %%a in (somefile.txt) do (
set key=a
echo /!key!/ /!arr[%key%]!/
)

And code above not work correctly. I mean that the key is display correct but value of !arr[%key%]! is empty (i don't know why).

When i try it like this:

@echo off
setlocal EnableDelayedExpansion

set arr[a]=1
set arr[b]=2

set key=a

for /F "tokens=*" %%a in (somefile.txt) do (
echo /!key!/ /!arr[%key%]!/
)

Code above work good. No idea why first code doesn't work and second work. Any ideas ?

user3139560
  • 19
  • 1
  • 3
  • See: [Arrays and other data structures in Batch script](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Jan 09 '15 at 16:48

1 Answers1

0
@echo off
setlocal EnableDelayedExpansion

set arr[a]=1
set arr[b]=2

for /F "tokens=*" %%a in (q27861004.txt) do (
 set key=%%a
 echo /!key!/ /!arr[%%a]!/
)

GOTO :EOF

I used a file named q27861004.txt containing this data for my testing.

a
b

Your problem is that %kay% is evaluated at parse-time, replacing %key% with its value at that time, being empty, hence displaying !arr[]! also empty

Magoo
  • 77,302
  • 8
  • 62
  • 84