A table is just a type of variable, just like a number, Boolean, string, or function. So if you store the tables within a table, you can use a variable as a table name. In fact, you can even use variables like functions or tables as variable names - as long as they’re stored in other tables. To keep a “database” of variables that will be dynamically changing, you should use a table. There really aren’t many languages that allow you to directly set the symbol name of a variable, because that can end up making things extremely slow. You’d basically be making every single variable global.
Also, Lua has no concept of “names”. A variable is “named” whatever you want it to be named, as long as it’s a valid L-value (something that can go on the left side of an assignment statement). For example:
[lua]
local thingy = {}
local otherNameForThingy = thingy
local yetAnotherNameForThingy = otherNameForThingy
local function beReallyHappy(twizzler)
print(“I’m happy!”)
end
beReallyHappy(yetAnotherNameForThingy)
[/lua]
In the above example, after it’s run, thingy, otherNameForThingy, yetAnotherNameForThingy, and twizzler all refer to the same variable. The only thing that matters to Lua is that the name for the variable is a valid L-value, and the value for the variable is a valid R-value.
A concatenation expression is not an L-value. It’s an R-value. However, a table access expression is a valid L-value. So you can do this:
[lua]
test[“variable” … i] = {}
[/lua]
But not this:
[lua]
“variable” … i = {}
[/lua]
This is a simple matter of Lua’s syntax. In a lot of languages, you’ll get an error that goes something like “r-value in assignment” or something to that effect, but Lua gives you “unexpected symbol”.
If Lua started allowing R-values as variable names, think about this:
[lua]
58 = 59
[/lua]
Though perhaps more obvious, 58 is an R-value just like “variable” … i. If R-values were valid L-values, and you set 58 to 59, you could completely destroy every math equation in the world. Or, in your case, what happens when we try to append the string “variable” … i onto a string?
[lua]
str = str … “variable” … i – Does “variable” … i refer to a variable? Or a string? Or something else?
[/lua]