0

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.

LeoDog896
  • 3,472
  • 1
  • 15
  • 40
  • No I do not see delayed expansion enbaled in your program. Regardless I think this is what you are trying to do. `set SlotChosen=!slot%input%!`. You only need one set of percent symbols to expand slotchosen. – Squashman Feb 19 '17 at 21:32
  • Thank you for your answer :D – LeoDog896 Feb 19 '17 at 21:39
  • You could do it this way as well, `call set SlotChosen=%%slot%input%%%` – Squashman Feb 19 '17 at 21:58
  • At [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) this management is fully explained. – Aacini Feb 19 '17 at 23:40

1 Answers1

2

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