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,
}
}
}