Trying to compare 2 lists (tables) and return true where any value is present in the new list which isn't in the old list. Struggling to do it, any help is appreciated!
An example which must return true:
Old = orange, apple, pear, mango
New = orange, apple, banana, pear
In this case, 'banana' is in the new list but not the old, so the code must return true.
Note: if there's a value in the old list that isn't in the new one, I still want to return false (as long as all new values are present in the old list).
example
Old = orange, apple, pear, mango
New = orange, apple, pear
In this case, all new values are in the old list so the code must return false. (I don't care that mango is in the old list but not the new one)
Tried different variations of ipairs
but haven't got this working so far.
CodePudding user response:
What you are looking to do is to compare sets. A set is a data structure that allows you to easily check for if a value is a member of that set. It is a very power full tool for programming.
local old = {"orange", "apple", "pear", "mango"}
local new = {"orange", "apple", "pear", "banana"}
local old_set = {}
for _, v in pairs(old) do
old_set[v] = true
end
for _, v in pairs(new) do
print(v, old_set[v] ~= true)
end
Output
orange false
apple false
pear false
banana true