如何批量迭代数组以获取key = value项

时间:2021-03-20 19:27:48

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.

我有一个定义为LIST =(a b c d e)的数组。 a,b,c,d,e被设置为系统变量,例如。 a = AAA,b = BBB等

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

在批处理脚本中,我想做一个for循环看起来像:

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.

我想要实现的是%% i(a)= %%% i%(%a%),它将被解析为系统变量,因此不会显示%a%,它将被解析为a = AAA 。

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

您是否知道如何在批处理脚本中执行此操作?

Thanks!

谢谢!

3 个解决方案

#1


11  

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

should solve your problem.

应该解决你的问题。

#2


3  

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

这是Lorenzo Donati的答案,但是稍微简单一点......

@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!

#3


1  

It wasn't very clear what you wanted to do. Try and see if this solves your problem:

你想做什么并不是很清楚。试着看看这是否解决了你的问题:

@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

:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
for  %%G in (%_list%) do (
    set  _name=%%G
    set  _value=!%%G!
    echo !_name! = !_value!
)

the script prints the name and the corresponding value of all the environment variables whose names are listed in the variable LIST.

该脚本打印名称以及名称在变量LIST中列出的所有环境变量的相应值。

#1


11  

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

should solve your problem.

应该解决你的问题。

#2


3  

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

这是Lorenzo Donati的答案,但是稍微简单一点......

@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!

#3


1  

It wasn't very clear what you wanted to do. Try and see if this solves your problem:

你想做什么并不是很清楚。试着看看这是否解决了你的问题:

@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

:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
for  %%G in (%_list%) do (
    set  _name=%%G
    set  _value=!%%G!
    echo !_name! = !_value!
)

the script prints the name and the corresponding value of all the environment variables whose names are listed in the variable LIST.

该脚本打印名称以及名称在变量LIST中列出的所有环境变量的相应值。