Referencing a table with a string in Lua

3

I need to use a table in Lua, but I can only reference it through a string, for example:

tabelaA = { "qualquer coisa" }
variavelA = "tabelaA"

print(variavelA)
resultado: "qualquer coisa"

Keeping in mind that I can not reference this table directly, I'll get the string name.

    
asked by anonymous 04.06.2016 / 07:05

1 answer

3

There is the _G table, called the global environment , which represents the existing variables:

> print(tabelaA[1])
qualquer coisa
> print(_G["tabelaA"][1])
qualquer coisa

Also, you can always build another table:

tabelaVariaveis = {}
tabelaVariaveis["tabelaA"] = tabelaA

-- e pode acrescentar mais
tabelaB = { "outra coisa" }
tabelaVariaveis["tabelaB"] = tabelaB

and use it:

> print(tabelaVariaveis["tabelaA"][1])
qualquer coisa
    
04.06.2016 / 10:38