How to print a table using a string as table identifier?

Hello, I’m trying to print a table using string as table identifier. I know the best way is to directly call the table using itself as a variable, but I need to know if what I want to achieve is possible.

Script:

-- first version

local tab1 = {3,1,5} 
local tab2 = {5,3,1} 
local tab3 = {1,5,3}  
	
local selected = "tab3" -- must be string // totable()? like tostring()

print(table.concat(selected,", "))
-- other version

local Tb_1 = {"S1","S2","S3"} -- {"S1","S2","S3","S4"}
local Tb_2 = {"V1","V2","V3"}

table.insert("Tb_1","S4") -- don't work(reason string identifier of table)
print(table.concat(Tb_1,", ")) -- works fine

Output expected:

-- first version
> 1, 5, 3
-- other version
> S1, S2, S3, S4

Output reality:

-- first version
invalid argument #1 to 'concat' (table expected, got string)
-- other version
invalid argument #1 to 'insert' (table expected, got string)

Thanks for any feedback.

1 Like

Maybe?

local tab1 = {3,1,5} 
local tab2 = {5,3,1} 
local tab3 = {1,5,3}  

local tables = {
["tab1"] = tab1,
["tab2"] = tab2,
["tab3"] = tab3,
}

local StringThing = "tab2"
print(tables[StringThing])
1 Like

seems to work…

local tab1 = {1,2,3}
local tab2 = {4,5,6}
local tab3 = {7,8,9}

local tabs = {
tab1 = tab1,
tab2 = tab2,
tab3 = tab3,
}

local StringSelect = "tab2"
print(tabs[StringSelect])

Thanks for the support!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.