简体   繁体   中英

Batch, variable inside variable in a for loop

How to set a variable inside a variable in a for loop? When I execute this code the var[] is empty. Someone can help me?

@echo off & setlocal enabledelayedexpansion

set var[0]=aaa
set var[1]=bbb
set var[2]=ccc
set var[3]=ddd
set var[4]=eee

for /L %%g in (1,1,3) do (
set /a num=!RANDOM! %% 5
echo position: !num!
echo keyword: !var[%num%]! :: THIS LINE NOT WORKING
)

pause

You essentially have two options to get the double variable expansion you require. You can either use CALL or another FOR command.

call echo keyword: %%var[!num!]%%

or

FOR %%h in (!num!) do echo keyword: !var[%%h]!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM