how can i check if the array1 value contains a values from array2?
@luther's answer will not always work for your question..
If the arrays are different sizes, it completely fails.
If you have an array where similar element are not in the exact same index, it can return a false negative.
- for example
a = {'one', 'two'}; b = {'two', 'one'}
will return false
Using table.sort
to solve this would be a band-aid solution without fixing the real problem.
The function below will work with arrays of different sizes containing elements in any order
function array_compare(a, b)
for ia, va in ipairs(a) do
for ib, vb in ipairs(b) do
if va == vb then
print("matching:",va)
end
end
end
end
In array_compare
we go through all the combinations of elements in table a and table b, compare them, and print if they are equal.
ipairs(table)
uses index, value
(instead of just value
)
For example
local array1 = { 'friend', 'work', 'privat' }
local array2 = { 'apple', 'juice', 'privat' }
array_compare(array1, array2)
will print
matching: privat