Tristan F.-R.
Tristan F.-R.

Reputation: 4224

How to get the name of a variable using another variable?

How to get the name of a variable using another variable? Right now I am doing a little project named "Cube". It is a Survival Simulator. Anyway, I have encountered a problem, but if there is no way to make this possible, I will do it the long way. Anyway, here's my code (Batch)

:UseItem    
title Cube - Use Item    
cls    
echo Cube: Choose An Item To Use  
call %name%.bat  
echo      1. %slot1%  
echo      2. %slot2%  
echo      3. %slot3%  
echo      4. %slot4%  
echo      5. %slot5%  
echo      6. %slot6%  
echo      7. %slot7%  
echo      8. %slot8%  
echo      9. %slot9%  
set /p input=Choice:   
set SlotChosen=slot%input%  
if %%SlotChosen%%==BlueBerry goto EatBlue 

So What I'm trying to do is since SlotChosen would be slot(input of player), for example, slot1, slot1 would be surrounded with %, that way it gets the name of the variable. (This is only a clip of the code.) I also have enabledelayexpansion in my program, too.

Upvotes: 0

Views: 55

Answers (1)

Squashman
Squashman

Reputation: 14320

Two options for you.

One uses delayed expansion.

set SlotChosen=!slot%input%!

The other uses CALL with the SET command to get the extra phase of expansion.

call set SlotChosen=%%slot%input%%%

Upvotes: 2

Related Questions