Concatenate variable names with numbers in LUA

1

I have a problem, I need to do the following:

I have a variable that holds a number (x = 3 for example)

And I have a table like this:

tabela={}
tabela.var1='teste1'
tabela.var2='teste2'
tabela.var2='teste3'
tabela.var2='teste4'

I need to access this table by typing tabela.var with variable x (in case 3), it would look like this

print(tabela.var..x)

But the value of tabela.var is nil . Is there any way to make it just after the concatenation the program reads as a variable?

    
asked by anonymous 09.07.2018 / 23:52

1 answer

0

The expression

tabela.var1

is equivalent to

tabela["var1"]

So try

print(tabela["var"..x])

However, consider this other way:

tabela={}
tabela.var={}
tabela.var[1]='teste1'
tabela.var[2]='teste2'
tabela.var[2]='teste3'
tabela.var[2]='teste4'
    
10.07.2018 / 02:48