# Find difference between two tables

Is there a way to get the difference between two tables?

For example…

``````

local table1 = {"Cat", "Dog", "Fish"}
local table2 = {"Cat", "Fish"}

local tablediff = table.Diff( table1, table2 )

``````

tablediff would be {“Dog”}

Another example…

``````

local table1 = {{"Cat", "Dog", "Fish"}, {{"Ford1", "Ford2", "Ford3"}, "Holden"}}
local table2 = {{"Fish"}, {{"Ford1", "Ford3"}}}

local tablediff = table.Diff( table1, table2 )

``````

tablediff would be {{“Cat”, “Dog”}, {{“Ford2”}, “Holden”}}

Something like this may work.

``````

Diff = {}
for k,v in pairs(Table1) do
if !table.HasValue( Table2, v ) then
table.insert(Diff, v)
end
end

``````

[editline]2nd July 2013[/editline]

``````

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 )

``````

it works for the first one but not the second, is there a way to make it work recursively?

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

``````

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 = {}

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 )

``````

[editline]2nd July 2013[/editline]

Also in your test script you are making a function table.Diff, but calling table.compare

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?

``````

local table1 = {"Cat", "Dog", "Fish"}
local table2 = {"Fish", "Cat"}

local tablediff = table.Diff( table1, table2 )

``````

``````

local table1 = {{"Cat", "Dog", "Fish"}, {{"Ford1", "Ford2", "Ford3"}, "Holden"}}
local table2 = {{{"Ford1", "Ford3"}}, {"Fish"}}

local tablediff = table.Diff( table1, table2 )

``````

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.