1

Is there a method to name an environment variable dynamically using another environment variable in a batch file?

Something like

numplayers=3
char%numplayer%atk=12 
echo char3atk  

with output

12
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    Related: [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990) – aschipfl Apr 23 '18 at 15:35

2 Answers2

4

Given that you are unlikely to know beforehand the number assigned to %numplayers%, here are a few ways you can see the value of the variable:

Set "numplayers=3"
Set "char%numplayers%atk=12"
Call Echo %%char%numplayers%atk%%

 

Set "numplayers=3"
Set "char%numplayers%atk=12"
Set char%numplayers%atk

 

SetLocal EnableDelayedExpansion
Set "numplayers=3"
Set "char%numplayers%atk=12"
Echo !char%numplayers%atk!
Compo
  • 36,585
  • 5
  • 27
  • 39
1

It's quite straightforward:

SET numplayers=3
SET char%numplayers%atk=12
ECHO %char3atk%
uri2x
  • 3,162
  • 1
  • 11
  • 25