Calling variable based on string?

I am trying to figure out a way to do the following:

I have MANY tables, each containing a total of 5 strings. The strings are put onto buttons. When the button is pressed, it takes you to a new set of buttons, whose labels I want to be pulled from a table, whose variable name is equal to the name of the string whose button you pressed. I am trying to avoid having to make thousands of different buttons, and instead make a single button function that generates new buttons based on the table it selects.

Is there a way to call a variable, based on a string that matches it’s name exactly?

Basically, how I set it up originally, was there’s a currentTable, and when you select a button, it changes the currentTable variable to match the string connected to the button you press, however I absent mindedly failed to realize that I was not reassigning a table to my table variable in this process, but rather a string that was the equivalent of my variable name.

I hope that makes sense. Basically, is there a way to take a string such as “Food” and use it to call a table named Food, and assign it to my currentTable variable? [import]uid: 197365 topic_id: 35686 reply_id: 335686[/import]

Sure. Tables can be referenced with dot syntax:

myTable.fredFlintstone = true

You can also access the same variable like this:

myTable[“fredFlintstone”] = true

So you should be able to use a string to reference any table cell.
[import]uid: 199310 topic_id: 35686 reply_id: 141915[/import]

I think I might not have explained my question correctly or I’m misunderstanding what you mean…

I need to use a string to reference an entire table, not a cell within a table, as I have the cells within the table labeled in a standard 1-5 fashion, so the cell references remain constant. What changes with each button press, is the table from which the buttons need to pull their labels (strings). The function that generates the buttons could be altered so that each button, when pressed, changes the currentTable (the variable that is set equal to the table I want to call when creating the buttons) to the desired table, but then I would have to make a unique set of buttons for every table, because every button leads to another table as described by the string, and because I will eventually have thousands of tables, that seems like a very clunky and time consuming way to do it. If that explanation made it more confusing, just ignore everything except for the first two sentences of this paragraph lol.

But yeah, I somehow need to use a string to reference a table. I’m not sure if that is possible :confused:

[import]uid: 197365 topic_id: 35686 reply_id: 141923[/import]

I’m pretty sure . Tables can contain other tables. So you could have:

tablea = {}  
tableb = {}  
tablec = {}  
  
masterTable = {}  
masterTable["tablea"] = tablea  
masterTable["tableb"] = tableb  
masterTable["tablec"] = tablec  

Does that help? [import]uid: 199310 topic_id: 35686 reply_id: 141924[/import]

Genius! That worked exactly like I needed it to! Thank you so much! [import]uid: 197365 topic_id: 35686 reply_id: 141930[/import]

Sure. Tables can be referenced with dot syntax:

myTable.fredFlintstone = true

You can also access the same variable like this:

myTable[“fredFlintstone”] = true

So you should be able to use a string to reference any table cell.
[import]uid: 199310 topic_id: 35686 reply_id: 141915[/import]

I think I might not have explained my question correctly or I’m misunderstanding what you mean…

I need to use a string to reference an entire table, not a cell within a table, as I have the cells within the table labeled in a standard 1-5 fashion, so the cell references remain constant. What changes with each button press, is the table from which the buttons need to pull their labels (strings). The function that generates the buttons could be altered so that each button, when pressed, changes the currentTable (the variable that is set equal to the table I want to call when creating the buttons) to the desired table, but then I would have to make a unique set of buttons for every table, because every button leads to another table as described by the string, and because I will eventually have thousands of tables, that seems like a very clunky and time consuming way to do it. If that explanation made it more confusing, just ignore everything except for the first two sentences of this paragraph lol.

But yeah, I somehow need to use a string to reference a table. I’m not sure if that is possible :confused:

[import]uid: 197365 topic_id: 35686 reply_id: 141923[/import]

I’m pretty sure . Tables can contain other tables. So you could have:

tablea = {}  
tableb = {}  
tablec = {}  
  
masterTable = {}  
masterTable["tablea"] = tablea  
masterTable["tableb"] = tableb  
masterTable["tablec"] = tablec  

Does that help? [import]uid: 199310 topic_id: 35686 reply_id: 141924[/import]

Genius! That worked exactly like I needed it to! Thank you so much! [import]uid: 197365 topic_id: 35686 reply_id: 141930[/import]