How could I check if a table (as array) contains all the keys of another table?
Something like array_diff_key in PHP:
https://www.php.net/manual/en/function.array-diff-key.php
This function should return, for example, ["firstLevel"]["e"] because this element doesn't exist in tblTwo:
tblOne = {
["firstLevel"] = {
["a"] = "something",
["b"] = "something",
["c"] = "something",
["subLevel"] = {
["d"] = "something",
["e"] = "something",
}
}
tblTwo = {
["firstLevel"] = {
["a"] = "something",
["b"] = "something",
["c"] = "something",
["subLevel"] = {
["d"] = "something",
}
}
Try the code below:
function diff(a,b,s)
for k,v in pairs(a) do
if b[k]==nil then return s.."."..k end
if type(v)=="table" then
local w=diff(v,b[k],k)
if w~=nil then return s.."."..w end
end
end
end
print(diff(tblOne,tblTwo,"TOP"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With