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 .
Otoris
Prole
Posts: 5 Joined: Thu Mar 15, 2012 6:38 am
Post
by Otoris » Thu Mar 15, 2012 6:43 am
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
nevon
Commander of the Circuloids
Posts: 938 Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:
Post
by nevon » Thu Mar 15, 2012 7:13 am
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.
shingo
Prole
Posts: 12 Joined: Tue Nov 22, 2011 2:55 am
Post
by shingo » Thu Mar 15, 2012 7:23 am
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.
Otoris
Prole
Posts: 5 Joined: Thu Mar 15, 2012 6:38 am
Post
by Otoris » Thu Mar 15, 2012 7:29 am
So there is no way to tell what the parent table is then?
shingo
Prole
Posts: 12 Joined: Tue Nov 22, 2011 2:55 am
Post
by shingo » Thu Mar 15, 2012 7:39 am
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
trubblegum
Party member
Posts: 192 Joined: Wed Feb 22, 2012 10:40 pm
Post
by trubblegum » Thu Mar 15, 2012 7:54 am
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
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Thu Mar 15, 2012 8:48 am
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 )
trubblegum
Party member
Posts: 192 Joined: Wed Feb 22, 2012 10:40 pm
Post
by trubblegum » Thu Mar 15, 2012 10:21 am
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.
Otoris
Prole
Posts: 5 Joined: Thu Mar 15, 2012 6:38 am
Post
by Otoris » Thu Mar 15, 2012 10:24 am
Thanks for all the advice, I'll just pass the parent table along with the child table in the function in that case!
Users browsing this forum: Bing [Bot] and 2 guests