I have a script that is supposed to compare/contrast two different arrays. It should determine if the arrays are equal or not and then also remove any functions. However the problem is that the code below is saying that the arrays are EQUAL when they are clearly not. So I was wondering how I can fix this
local table1 = {
test = 9;
}
local table2 = {
test = 9;
sight = {
rumn =9;
};
}
function comp(t1, t2)
local r = true
for i, v in pairs(t1) do
--// this is the value in the second table
local t2v = t2[i]
--// automatically break because the index in t2 is nil
--// and the index in t1 is not nil
if t2v == nil then
r = false
break
end
--// check if values are the same tables
if type(v) == "table" and type(t2v) == "table" then
if comp(v, t2v) == false then
r = false
break
end
--// ignore and remove functions
elseif type(v) == "function" and type(t2v) == "function" then
t1[i] = nil
t2[i] = nil
--// check if regular values are the same
elseif v ~= t2v then
r = false
break
end
end
--// return the result boolean
return r
end
--// is different in each table
local sameTables = comp(table1, table2)
print(sameTables) --// prints false because they are not the same
It’s always going to be true because it will only compare the numbers i.e test, test and rumn (all have the same value) and then the code stops since the condition has finished and there has been no results. EDIT: it’s always true since the next value is also 9, if you change it to any other number it would be false.
function comp(t1, t2)
local r = true
for i, v in pairs(t1) do
--// this is the value in the second table
local t2v = t2[i]
--// automatically break because the index in t2 is nil
--// and the index in t1 is not nil
if t2v == nil then
r = false
break
end
--// check if values are the same tables
if type(v) == "table" and type(t2v) == "table" then
if comp(v, t2v) == false then
r = false
break
end
--// ignore and remove functions
elseif type(v) == "function" and type(t2v) == "function" then
t1[i] = nil
t2[i] = nil
--// check if regular values are the same
elseif v ~= t2v then
r = false
break
end
end
for i, v in pairs(t2) do
local t2v = t1[i]
--// automatically break because the index in t2 is nil
--// and the index in t1 is not nil
if t2v == nil then
print(t2v)
r = false
break
end
end
--// return the result boolean
return r
end