Get the child table of another table having the string name of the child table

local table1 = {
      tab = {
       val1,
       val2
   }
}

i want to refer to tab having the string “tab”

1 Like
table1 = {
    tab = {
        "tab",
    },
}

print(table1.tab[1])
-- prints tab

table2 = {
    tab = {
        tabString = "tab",
    },
}

print(table2.tab.tabString)
-- prints tab

I wanted to refer to the tab table, i already have the “tab” string. I tried storing “tab” in a variable but it’s useless:

local name = "tab"

local tabTable = table1.name

if you want to refer to the tab table, all you have to do is table1.tab

if you want to assign a name to the tab table, you’ll need to have it as a variable inside of itself

for example:

table1 = {
    tab = {
        Name = "tab",
        description = "abababab",
    },
}

print(table1.tab.Name)
-- prints "tab"

table1.tab.Name = "abc"
print(table1.tab.Name)
-- prints abc

I want to refer to the tab in table1, but in table1 there are more tables, so i need to work with variables

table1 = {
    tab = {
        Name = "tab",
        description = "abababab",
    },
}
local name = "tab"
print(table1[name])
3 Likes

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