Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over array in batch for key=value item

I have an array defined as LIST=(a b c d e). The a, b, c, d, e are set as system variables, eg. a=AAA, b=BBB, etc.

In a batch script, I would like to do a for loop looking like:

for %%i in %LIST% do echo %%i=%%%i% (unfortunately, this doesn't work)

What I want to achieve is that %%i (a) = %%%i% (%a%), which will be resolved as system variable, thus instead of showing %a%, it'll be resolved as a=AAA.

Do you have any idea how to do it in a batch script?

Thanks!

like image 961
Honza Sestak Avatar asked Aug 25 '13 22:08

Honza Sestak


2 Answers

for %%i in %LIST% do CALL echo %%i=%%%%i%%

should solve your problem.

like image 91
Magoo Avatar answered Oct 22 '22 11:10

Magoo


This is the same answer of Lorenzo Donati, but in a slightly simpler way...

@echo off
setlocal enabledelayedexpansion
set LIST=(a b c d e)
set a=value of A
set b=value of B
set c=value of C
set d=value of D
set e=value of E

for %%G in %LIST% do echo %%G = !%%G!
like image 43
Aacini Avatar answered Oct 22 '22 11:10

Aacini