Getting the key table?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Otoris
Prole
Posts: 5
Joined: Thu Mar 15, 2012 6:38 am

Getting the key table?

Post 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 ^^
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Getting the key table?

Post by nevon »

That should probably work. EDIT: Oh, nevermind. I thought you were running the function on Table1, not Table1.innertable.
Last edited by nevon on Thu Mar 15, 2012 7:44 am, edited 1 time in total.
User avatar
shingo
Prole
Posts: 12
Joined: Tue Nov 22, 2011 2:55 am

Re: Getting the key table?

Post 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.
User avatar
Otoris
Prole
Posts: 5
Joined: Thu Mar 15, 2012 6:38 am

Re: Getting the key table?

Post by Otoris »

So there is no way to tell what the parent table is then?
User avatar
shingo
Prole
Posts: 12
Joined: Tue Nov 22, 2011 2:55 am

Re: Getting the key table?

Post 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
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Getting the key table?

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Getting the key table?

Post 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 )
Help us help you: attach a .love.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Getting the key table?

Post 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.
Last edited by trubblegum on Thu Mar 15, 2012 10:25 am, edited 1 time in total.
User avatar
Otoris
Prole
Posts: 5
Joined: Thu Mar 15, 2012 6:38 am

Re: Getting the key table?

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 21 guests