0

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
user3486019
  • 163
  • 6

1 Answers1

2

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]!
Squashman
  • 13,649
  • 5
  • 27
  • 36