Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1091 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to iterate over array in batch for key=value item

#1
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!
Reply

#2
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`.
Reply

#3
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!

Reply

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

should solve your problem.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through