0

I have been making a menu system but I've run into a problem: I have to use a variable that keeps track of the selected item, to specify the item I want to use in my list. I understand that this question has been asked before, but I haven't found a suitable answer that works yet

set new_option[%selected%]=%selection_symbol%%org_option[%selected%]%

The nested variable is "%org_option[%selected%]%"

The problem is that batch splits the the variable into "%org_option[%", "selected", and "%]%" but I want it to be split up so that the "selected" variable is used to define the position in the list, meaning that "selected" is nested in "org_option[]"

Jamie Bowen
  • 55
  • 1
  • 8
  • 1
    It is explained in [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). – luciole75w Nov 23 '19 at 04:20

1 Answers1

2

You essentially are trying to reference an array variable. You have two options to solve this. One is to use the CALL command to get an extra layer of variable expansion which requires you to double the percent symbols on the outer variable and the other requires you to enable delayed expansion in your code and reference the outer variable with exclamation marks.

 CALL set new_option[%selected%]=%selection_symbol%%%org_option[%selected%]%%

OR

 Setlocal enabledelayedexpansion
 set new_option[%selected%]=%selection_symbol%!org_option[%selected%]!
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 1
    I will probably end up removing this answer as luciole75w pointed out this really is a duplicate question. So I have started the vote process to have the question closed. – Squashman Nov 23 '19 at 04:35
  • Your first example about call, outside of a for/if block, looks actually clearer than in the linked answer. In my opinion your answer is still interesting, I'd keep it. – luciole75w Nov 25 '19 at 01:46