Hello! I am trying to develop helper functions for a game that I am making. This helper function
checks if two dictionaries have the same shared values. It works well but I want to know if there is
a more efficient solution to this or if there is a built in function that I am unaware of.
Thanks!
dictionariesEqual = function(dictionary1, dictionary2)
for i,v in dictionary1 do
if typeof(dictionary1[i]) == "table" then
if not dictionariesEqual(dictionary1[i],dictionary2[i]) then
return false
end
else
if not (dictionary1[i] == dictionary2[i]) then
return false
end
end
end
return true
end
This function will error if the first table has a table key and the second table does not have a correlating one, because you are only checking if the first table’s value is a table.
There’s not much else I can add, other than some basic optimizations. There’s also a good library that has typings in it you can look into.
local function AreTablesEqual(Table1: {any}, Table2: {any}): boolean
for Key, Value1 in Table1 do
local Value2 = Table2[Key]
if type(Value1) == "table" and type(Value2) == "table" then
if not AreTablesEqual(Value1, Value2) then
return false
end
elseif Value1 ~= Value2 then
return false
end
end
return true
end