• Find difference between two tables
    6 replies, posted
Is there a way to get the difference between two tables? For example... [code] local table1 = {"Cat", "Dog", "Fish"} local table2 = {"Cat", "Fish"} local tablediff = table.Diff( table1, table2 ) [/code] tablediff would be {"Dog"} Another example... [code] local table1 = {{"Cat", "Dog", "Fish"}, {{"Ford1", "Ford2", "Ford3"}, "Holden"}} local table2 = {{"Fish"}, {{"Ford1", "Ford3"}}} local tablediff = table.Diff( table1, table2 ) [/code] tablediff would be {{"Cat", "Dog"}, {{"Ford2"}, "Holden"}}
Something like this may work. [code] Diff = {} for k,v in pairs(Table1) do if !table.HasValue( Table2, v ) then table.insert(Diff, v) end end [/code] [editline]2nd July 2013[/editline] [url]http://wiki.garrysmod.com/page/table/HasValue[/url]
[code] local table1 = {"Cat", "Dog", "Fish"} local table2 = {"Cat", "Fish"} local table3 = {{"Cat", "Dog", "Fish"}, {{"Ford1", "Ford2", "Ford3"}, "Holden"}} local table4 = {{"Fish"}, {{"Ford1", "Ford3"}}} function table.Diff( tbl1, tbl2, diff ) for k,v in pairs( tbl1 ) do if !table.HasValue( tbl2, v ) then table.insert( diff, v ) end end end diff = {} table.Compare( table1, table2, diff ) PrintTable( diff ) print("===========================") diff = {} table.Compare( table3, table4, diff ) PrintTable( diff ) [/code] it works for the first one but not the second, is there a way to make it work recursively? [IMG]http://i.imgur.com/sZQ6GCD.jpg[/IMG]
Tables in tables makes things difficult, I couldn't help anymore sorry :\ [editline]2nd July 2013[/editline] Try something like this, If this doesn't work then I'm done :P [code] local table1 = {"Cat", "Dog", "Fish"} local table2 = {"Cat", "Fish"} local table3 = {{"Cat", "Dog", "Fish"}, {{"Ford1", "Ford2", "Ford3"}, "Holden"}} local table4 = {{"Fish"}, {{"Ford1", "Ford3"}}} function table.FindTable(Atable) do local DiffTableCheck = {} local Checked = {} for k,v in pairs(Atable) do if istable( v ) then table.insert(DiffTableCheck, v) table.FindTable(DiffTableCheck) else table.insert(Checked , v) end end return Checked end function table.Diff( tbl1, tbl2 ) local diff = {} local c_tbl1 = {} local c_tbl2 = {} table.Add(c_tbl1,table.FindTable(tbl1)) table.Add(c_tbl2,table.FindTable(tbl2)) for k,v in pairs( c_tbl1 ) do if !table.HasValue( c_tbl2, v ) then table.insert( diff, v ) end end for k,v in pairs( c_tbl2 ) do if !table.HasValue( c_tbl1, v ) and !table.HadValue(diff, v) then table.insert( diff, v ) end end end local Derp = table.Diff( table1, table2) PrintTable( Derp ) print("===========================") local Derp2 = table.Diff( table3, table4) PrintTable( Derp2 ) [/code] [editline]2nd July 2013[/editline] Also in your test script you are making a function table.Diff, but calling table.compare
[QUOTE=Pandaman09;41273435]Tables in tables makes things difficult, I couldn't help anymore sorry :\ [editline]2nd July 2013[/editline] Try something like this, If this doesn't work then I'm done :P [code] local table1 = {"Cat", "Dog", "Fish"} local table2 = {"Cat", "Fish"} local table3 = {{"Cat", "Dog", "Fish"}, {{"Ford1", "Ford2", "Ford3"}, "Holden"}} local table4 = {{"Fish"}, {{"Ford1", "Ford3"}}} function table.FindTable(Atable) do local DiffTableCheck = {} local Checked = {} for k,v in pairs(Atable) do if istable( v ) then table.insert(DiffTableCheck, v) table.FindTable(DiffTableCheck) else table.insert(Checked , v) end end return Checked end function table.Diff( tbl1, tbl2 ) local diff = {} local c_tbl1 = {} local c_tbl2 = {} table.Add(c_tbl1,table.FindTable(tbl1)) table.Add(c_tbl2,table.FindTable(tbl2)) for k,v in pairs( c_tbl1 ) do if !table.HasValue( c_tbl2, v ) then table.insert( diff, v ) end end for k,v in pairs( c_tbl2 ) do if !table.HasValue( c_tbl1, v ) and !table.HadValue(diff, v) then table.insert( diff, v ) end end end local Derp = table.Diff( table1, table2) PrintTable( Derp ) print("===========================") local Derp2 = table.Diff( table3, table4) PrintTable( Derp2 ) [/code] [editline]2nd July 2013[/editline] Also in your test script you are making a function table.Diff, but calling table.compare[/QUOTE] your script didn't work :/
What do you need this for? Because it looks rather vague, do the tables need to follow the same order or not? For instance, what would this result in? [code] local table1 = {"Cat", "Dog", "Fish"} local table2 = {"Fish", "Cat"} local tablediff = table.Diff( table1, table2 ) [/code] And how about this? [code] local table1 = {{"Cat", "Dog", "Fish"}, {{"Ford1", "Ford2", "Ford3"}, "Holden"}} local table2 = {{{"Ford1", "Ford3"}}, {"Fish"}} local tablediff = table.Diff( table1, table2 ) [/code]
I would guess he wants something like this : [lua] function table.Diff( tab, tab2 ) local diff = {}; for key, val in pairs( tab ) do if ( !tab2[ key ] || !tab2[ key ] == v ) then // Index the difference table by incrementing numbers if the compared tables are also indiced by numbers, otherwise use original indicies. diff[ (isnumber( key ) and #diff + 1 or key) ] = v; end end return diff; end [/lua] Because I can't think of a bunch of uses checking for differences recursively.
Sorry, you need to Log In to post a reply to this thread.