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
3
  • Give this a try: call echo keyword: %%var[!num!]%% Commented Nov 10, 2017 at 0:56
  • Thanks Squashman. This works. Commented Nov 10, 2017 at 1:00
  • This management is fully explained at this answer Commented Nov 10, 2017 at 20:16

1 Answer 1

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]!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.