I wanna check if two tables have the same value in Lua, but didn\'t find the way.
I use the operator ==
, it seems just to check the same objects, but no
I currently use this
local tableCompare
do
local compare
compare = function(src, tmp, _reverse)
if (type(src) ~= "table" or type(tmp) ~= "table") then
return src == tmp
end
for k, v in next, src do
if type(v) == "table" then
if type(tmp[k]) ~= "table" or not compare(v, tmp[k]) then
return false
end
else
if tmp[k] ~= v then
return false
end
end
end
return _reverse and true or compare(tmp, src, true)
end
tableCompare = function(src, tmp, checkMeta)
return compare(src, tmp) and (not checkMeta or compare(getmetatable(src), getmetatable(tmp)))
end
end
print(tableCompare({ 1 , b = 30 }, { b = 30, 1 }, false))