Check if 2 nested dictionaries are equal

Hi, I want to create a function that checks if 2 tables are equal:

function TablesAreEqual(table1, table2)
    for index1, value1 in pairs(table1) do
        --if table 2 does not contain key
        if not table2[index1] then
            return false
        end
        --if value1 is a table, compare the tables
        if type(value1) == "table" and not next(value1) then
            Util.TablesAreEqual(value1, table2[index1])
            continue
        end
        --if value1 is not table, compare values
        if table1[index1] ~= table2[index1] then
            return false
        end
    end
    return true
end

Here are 3 sample tables. table1 and table2 are the same, whereas table3 is different. The function doesn’t return true when I pass in table1 and table2 as the arguments. Not sure why!

local table1 = {
    ["test"] = 1,
    ["test2"] = "1",
    ["test3"] = true,
    ["test4"] = {
        ["test5"] = {
            ["test6"] = "1",
            ["test7"] = true,
        }
    }
}
local table2 = {
    ["test"] = 1,
    ["test2"] = "1",
    ["test3"] = true,
    ["test4"] = {
        ["test5"] = {
            ["test6"] = "1",
            ["test7"] = true,
        }
    }
}
local table3 = {
    ["test"] = 1,
    ["test2"] = "1",
    ["test3"] = true,
    ["test4"] = {
        ["test5"] = {
            ["test6"] = "2",
            ["test7"] = false,
        }
    }
}
2 Likes

This is my idea :_:

local C = {}
for i,a in pairs(table1) do
for i,b in pairs(table2) do
if a == b then
table.insert(C, a)
end
end
end
if #a == #C then print("=") end
1 Like

Here’s how I would approach it:

local function Equals(t1, t2)
	if #t1 ~= #t2 then return false end
	for i,v in pairs(t1) do
		if t2[i] then
			if type(v) == "table" and type(t2[i]) == "table" then
				if not Equals(v, t2[i]) then
					return false
				end
			else
				if v~=t2[i] then
					return false
				end
			end
		else
			return false
		end
	end
	return true
end

Notice how the function is recursive (the function calls itself within its body) So your nested tables can go into depth as much as you need.

3 Likes

When I ran
local table1 = {
[“akak”] = {
[“candyy”] = 29
}
}
local table2 = {
[“akak”] = {
[“candyy”] = 29,
[“candyys”] = 29
}
}
print(Equals(table1, table2))
it returns true that they are equal when table2 has an extra key.
So this doesn’t work fully

1 Like

@OttodotOfficial hello this topic is old but yeah if anyone needs it:

local function CountTable(tbl)
	local count = 0
	for i,v in pairs(tbl) do
		count += 1
	end
	return count
end

local function Equals(t1, t2)
	if CountTable(t1) ~= CountTable(t2) then return false end
	for i,v in pairs(t1) do
		if t2[i] ~= nil then
			if type(v) == "table" and type(t2[i]) == "table" then
				if not Equals(v, t2[i]) then
					return false
				end
			else
				if v~=t2[i] then
					return false
				end
			end
		else
			return false
		end
	end
	return true
end

I fixed two bugs, enjoy
(thanks @Bou_Baker for the approach)

1 Like