Using a Variable to Select Which Variable to Use

I have a situation in my game where I want to get the value of a variable. However, I don't know which variable I want to get the value of. Is there a way to use a variable to select which variable to operate on? For example, if variable 7 is equal to 21, then I want to use variable 21. If V[7] = 22, then I want to use V[22]. And so on.

Obviously, I could just do 12 if statements to get the desired effect, but I'm hoping there's a more concise solution. Being able to use a variable to select a variable seems like it should be a simple thing, but I can't figure out how to do it.

********

I was just about to post this question when I saw the Script Call List thread and decided to dig a bit.

Using a script call, the solution I was looking for is:
$gameVariables.setValue(7,$gameVariables.value($gameVariables.value(7)))

To use examples from my project, V[7]'s value is equal to the current region ID + 20, so in region 1 V[7] = 21 and V[21] = 1. After running the above code, V[7] = 1 as intended.
V[22] = 0 and V[23] = 2, so if I ran the same code in the other regions, I'd end up with V[7] = 0 or 2 as appropriate.

I'm still curious if there was a simpler way to do this that I missed, and I figured that my solution might be helpful to other people, so despite answering my own question I still decided to post this.
no, you'll have to use a script call.
indirect referencing had been removed from the event system with RMXP (2K/2k3 were the last makers to have it) and not returned despite how usefull it had been
You could use a switch case

switch($gameVariables.value()){
case 7: 21;break;
case 8: 318;break;
case 9: 56;break;
}}

Edit: Sorry, the example text might not be right because your explanation is confusing, but generally a bunch of if statements ala "I could just do 12 if statements to get the desired effect" can be shortened to a switch case.
@AquaEcho In a switch, each case needs a complete, valid code statement. So you'd have to do something like:
Code:
switch ($gameVariables.value(7))
{
    case 7:
        temp = 21;
        break;
    case 8:
        temp = 318;
        break;
etc.
}
$gameVariables.setValue(temp, value_to_put_there);

However, I don't get the reason to go through any of that when mauve's example is literally just taking an existing variable's value. They don't appear to be extrapolating a different value from it.

@mauvebutterfly I had a hard time figuring through it at first, but that looks correct. I'm not sure why you can't just use $gameVariables.value(7)-20
Yeah, my bad, I left it unfinished because I was confused as to what to put there from original post.
I'm still curious if there was a simpler way to do this that I missed
If it comes to just setting variable 7 to the value of another variable with the index equal to the value of variable 7, that line would indeed be the simplest way to do this.

When it comes to this entire system, it depends exactly on what it does and how many regions it uses, but with enough of them it might be simpler to use an array to store all those value rather than a separate variable for each.
I had a hard time figuring through it at first, but that looks correct. I'm not sure why you can't just use $gameVariables.value(7)-20

Sorry, I'll try to be clearer with what I'm doing.

V[21] stores the number of enemies that exist in region 1. I'm using V[7] to determine which troop battle to run. If V[21] is 1, I want to run troop 1 which contains only one enemy. If V[21] is 2, I want to run troop 2 which contains two enemies. I can't use $gameVariables.value(7)-20 because that would get me the region ID again instead of getting me the number of enemies in the region. I guess the confusion is that I'm using region IDs 1 through 12, but I'm using variables 21 through 32 to store the contents of the regions.

I have 12 separate regions on my map and the player has some limited ability to move enemies between them. This allows the player to control how many enemies they will have to fight to get to the goal.

The reason I did all this variable manipulation was to create a generalised event that I can copy and paste without needing to modify it. This event triggers every time the player enters a new region. It was a much simpler event before I used these script calls, but the problem was that every time I copied the event I needed to manually change it to conform to the new region. The current version handles that automatically, so I can copy it into new regions and use it without needing to change the event at all.

Spoiler: Code Screenshot
VariableVariable.png

If it comes to just setting variable 7 to the value of another variable with the index equal to the value of variable 7, that line would indeed be the simplest way to do this.

When it comes to this entire system, it depends exactly on what it does and how many regions it uses, but with enough of them it might be simpler to use an array to store all those value rather than a separate variable for each.
My current system allows for up to 20 regions and I could block off space for more, but you're right that an array would be a better way to handle this. I'm concerned because I used an array before and I think I hit some sort of limit on how many things I could store in a single variable because it didn't end up working after getting to a certain size. That was a 3D matrix holding X,Y,Z coordinates for the rooms of a randomly generated dungeon though, so it's probably a lot more data than what I'm doing here.

I was considering doing arrays within each variable for counting enemies of different types, but now I'm thinking it would be better to store each enemy type as a separate variable and to use an array within each variable to store the number of that enemy in each region. That way if I ever do make a dungeon with 21 or more regions I won't have random variables in weird places that I need to compensate for.

I'll rewrite the thing again to use arrays as a future-proofing measture. Thanks for the suggestion.
I don't know how much work you've already put into this, but it might be easier to use an existing plugin.

Original thread: https://forums.rpgmakerweb.com/threads/using-a-variable-to-select-which-variable-to-use.156781/