Page 1 of 1

Getting the key table?

Posted: Thu Mar 15, 2012 6:43 am
by Otoris
I think I phrased that correctly... I didn't did I?

Here is an example of what I'm trying to do since my programming is rusty:

Code: Select all

//Tables with Values
Table1 = { innerTable = {} }
Table2 = { innerTable = {} }
//My function
function CheckSource(table)
    if table == Table1 then
         STUFF
    elseif table == Table2 then
        OTHER STUFF
    end
end
//Now to call it
CheckSource( Table1.innerTable )

Any help is much appreciated ^^

Re: Getting the key table?

Posted: Thu Mar 15, 2012 7:13 am
by nevon
That should probably work. EDIT: Oh, nevermind. I thought you were running the function on Table1, not Table1.innertable.

Re: Getting the key table?

Posted: Thu Mar 15, 2012 7:23 am
by shingo
There're totally 4 tables,
Table1
Table2
Table1.innerTable
Table2.innerTable

So neither STUFF nor OTHER STUFF will run, Table1.innerTable is equal to itself only.

Re: Getting the key table?

Posted: Thu Mar 15, 2012 7:29 am
by Otoris
So there is no way to tell what the parent table is then?

Re: Getting the key table?

Posted: Thu Mar 15, 2012 7:39 am
by shingo
Otoris wrote:So there is no way to tell what the parent table is then?
Record it by yourself, I think that's the easiest way.

Code: Select all

Table = {innerTable = {}}
Table.innerTable.parent = Table

Re: Getting the key table?

Posted: Thu Mar 15, 2012 7:54 am
by trubblegum
You could do it procedurally by putting table1 and table2 in mastertable, and iterating through that.

Code: Select all

mastertable = {
   table1 = {
      table = {}
   }
   table2 = {
      table = {}
   }
}

function getparent(table)
   for i, t in pairs(mastertable) do
      if t.table == table then
         return t
      end
   end
end

Re: Getting the key table?

Posted: Thu Mar 15, 2012 8:48 am
by Robin
trubblegum wrote:You could do it procedurally by putting table1 and table2 in mastertable, and iterating through that.
While this works, I wouldn't do it, because it changes what could have been O(1) to O(n), where n = #mastertable.

So either provide an "parent link", like shingo showed, or pass the parent table:

Code: Select all

--Tables with Values -- // doesn't work in Lua.
    Table1 = { innerTable = {} }
    Table2 = { innerTable = {} }
    --My function
    function CheckSource(t)
        local innerTable = t.innerTable -- use innerTable in this function
        if t == Table1 then
             STUFF 
        elseif t == Table2 then
            OTHER STUFF
        end
    end
    --Now to call it
    CheckSource( Table1 )

Re: Getting the key table?

Posted: Thu Mar 15, 2012 10:21 am
by trubblegum
The problem is that we don't know what table1 is.
It could be table2.

.. if I'm understanding the problem.
If we already know table1 then why are we trying to find it?

unless innertable is table1 or table2 .. but why you'd want that, i can't imagine.

Re: Getting the key table?

Posted: Thu Mar 15, 2012 10:24 am
by Otoris
Thanks for all the advice, I'll just pass the parent table along with the child table in the function in that case!