Return Table from value inside that same table?

Hello, this is a very basic question, but I’m struggling to figure out how to achieve it. What I need is to figure out which table the value inside that table originates from.
Basically,

MainTable = {
    Table1 = {Val1,Val2,Val3};
    Table2 = {Val1,Val2,Val3};
    Table3 = {Val1,Val2,Val3};
}

In the script I find Val2. Now I need to know which table Val2 comes from, in this example Table2. I’m not sure how to find Table2 from Val2 inside of it. Any help is appreciated thanks in advanced.

If you just want to search through the tables and find the first one that contains the element Val2, then you could do something like this.

local returnFirstTable = function(parentTable,whatToFind)
    for index, subTable in pairs(parentTable) do
        if table.find(subTable,whatToFind) then
            return subTable
        end
    end
end

Looping through all the tables, then search each for the item in question and return the first table that has the item.

1 Like