My solution is not absolute (doesn't like keys), but should work with the nested tables that you specify. My concept is recursive and simple:
Take a record from each input, make sure that they: are of type, both are tables, and both tables are the same length. If these three things are true, you can now 1: 1 recursively compare two tables. If the types do not match or the tables have different lengths, this is an automatic failure.
function compare (one, two)
if type(one) == type(two) then
if type(one) == "table" then
if
-- If both types are the same, both are tables and
-- the tables are the same size, recurse through each
-- table entry.
for loop=1,
if compare (one[loop], two[loop]) == false then
return false
end
end
-- All table contents match
return true
end
else
-- Values are not tables but matching types. Compare
-- them and return if they match
return one == two
end
end
return false
end
do
t1 = {{1,1},{2,2}}
t2 = {{1,1},{2,2}}
t3 = {{1,1},{2,2},{3,3}}
print (string.format(
"t1 == t2 : %s",
tostring(compare (t1,t2))))
print (string.format(
"t1 == t3 : %s",
tostring(compare (t1,t3))))
end
Conclusion:
t1 == t2 : true
t1 == t3 : false
source
share