Using Variable as Table Name

Is any way to use a variable for the table name. I get error message from corona and it seems you cant use a variable as a table name. Does anyone know a way around this?

Please provide the code that’s causing an error.

for i=1,3 do

  i={}

end

1[1]=0

Hi @afarazmand,

This is not valid. You can’t name Lua variables as numbers, or even beginning with a number. For example, “t2” would be a valid variable name, but “2” or “2t” is not allowed.

Take care,

Brent

Sorry Brent, that was a randomcode I included but it doesnt answer the original issue. Please check the following which also gives an error message.

for i=1,3 do

  “test”…i={}

end

test1[1]=0

tables can be indexed by using a string value so yes, theoretically that is possible but you would have to do it like this

test = {} for i = 1,3 do     test[tostring(i)] = i end print(test["1"])

Thanks The Lemon. But I am not looking to use variable for index but the table Name itself such as below.

test = {}

test[1]=“test1”

test[2]=“test2”

“inbox”…test[1] ={}

There is a way. It’s an terrible way. You will consign your soul to eternal damnation and torment if you choose such a dark path. But because I believe in free choice, I will share this forbidden way with you. It is as follows.

\_G["inbox"..test..1]="test1" \_G["inbox"..test..2]="test2"

Thnaks Corona273. I noticed this but there was a problem that I couldnt retrieve them outside the function I was defining this, even though I defined a global variable. It also makes the size of one table quite big.

The only way to do what you want to do is to either use the global table (bad idea, in less powerful words than @corona273 used :D), or to use a local table. Don’t worry about tables being too big; that’s not a problem.

[lua]
local test = {}
for i = 1, 3 do
test[i] = {}
end
test[1][1] = 0
[/lua]

Seriously, it doesn’t matter how large your tables are. Even up to tens of thousands of elements. Lua can handle it.

  • Caleb

Does this mean you can only have a set number of tables in a Corona app (as you cant have variable as table name)?

No. You get 200 local variables per scope, and potentially infinite variables per table.

I suggest you get very well acquainted with the Lua programming language before getting started with Corona. Without a full grasp of the underlying language, you can’t get a full grasp of the tool. A good book for learning from the beginning is Programming in Lua, which can be found for free online here: http://www.lua.org/pil/1.html

  • Caleb

I mean the table name itself. Obviously you cant use a variable as the table name.

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]

  • Caleb

Please provide the code that’s causing an error.

for i=1,3 do

  i={}

end

1[1]=0

Hi @afarazmand,

This is not valid. You can’t name Lua variables as numbers, or even beginning with a number. For example, “t2” would be a valid variable name, but “2” or “2t” is not allowed.

Take care,

Brent

Sorry Brent, that was a randomcode I included but it doesnt answer the original issue. Please check the following which also gives an error message.

for i=1,3 do

  “test”…i={}

end

test1[1]=0

tables can be indexed by using a string value so yes, theoretically that is possible but you would have to do it like this

test = {} for i = 1,3 do     test[tostring(i)] = i end print(test["1"])

Thanks The Lemon. But I am not looking to use variable for index but the table Name itself such as below.

test = {}

test[1]=“test1”

test[2]=“test2”

“inbox”…test[1] ={}