I am making a script that compares (or contrasts, whatever you want to call it,) two tables and returns a true or false value based on if the tables are different or not. The function below is also supposed to remove any functions as well but for some reason the output is telling me that the tables are different even though they are the same.
local _FOLDER = {
Key = "DataFolder!",
ID = 043682999999000989149,
Lets = function()
end,
Test = {
Lost = {
Lost = {
Lost = {
Test = 9;
Best = 4;
};
};
};
}
}
local _FOLDER2 = {
Key = "DataFolder!",
ID = 043682999999000989149,
Lets = function()
end,
Test = {
Lost = {
Lost = {
Lost = {
Test = 9;
Best = 4;
};
};
};
}
}
function comp(arr1,arr2)
if #arr1 ~= #arr2 then return true end
for nam1, obj1 in pairs(arr1) do
if (typeof(obj1) == "table") then
if arr2[nam1] == nil then
return true
end
if (comp(arr2[nam1], obj1) == true) then
return true
end
else
if (typeof(obj1) == "function") then
obj1 = nil
end
if arr2[nam1] == nil then
print(nam1)
return true
end
if (obj1 ~= arr2[nam1]) then
print(obj1)
return true --
end
--
for nam2,obj2 in pairs(arr2) do
if arr1[nam2] == nil then
print(nam2)
return true
end
end
end
end
return false
end
local cse = comp(_FOLDER, _FOLDER2)
print(cse)
For a compare function, you really only need to compare indices and values of one table to another table. For example:
function comp(t1, t2)
--// the boolean thats returned
local r = true
--// loop through one table because if two tables are the same,
--// the tables should have the same indices and values
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
Now, using the function comp() and passing two tables, the return value will be a boolean and the tables will have the function values removed.
local table1 = {
x = 2,
y = 4,
z = 10,
f = function()
print("does something lol")
end
}
local table2 = {
x = 2,
y = 4,
z = 9,
f = function()
print("does something lol")
end
}
--// you can see that table1 and table2 are not the same because the index 'z'
--// is different in each table
local sameTables = comp(table1, table2)
print(sameTables) --// prints false because they are not the same
print(table1)
print(table2)
--// you can see that the function values of the tables have been removed
Testing this function with your given tables does return true, and removes the function indices.
NOTE: If you want the function to remove all function values regardless of whether the tables are equal, remove the breaks in the loop
this does not work because this will check whether the memory address to the tables are the same (not whether the index-value pairs inside the tables are the same).
A couple of reasons why your code doesn’t work as you wanted (others have already told you what will)
Note: in your case #arr1 and #arr2 are both 0 since only values with integer keys are counted.
You are not “removing” functions from the tables. In fact doing obj1 = nil only makes it so your code fails since obj1 = nil but arr2[nam1] is not, even though both are functions.
Consider A = {1} for i,j in pairs(A) do j=nil end print(A) still just prints the original A.
Just figured out another way to tell if 2 tables are identical:
local HTTPService = game:GetService("HttpService")
local Table1 = {} --Whatever
local Table2 = {} --Whatever
local Same = HTTPService:JSONEncode(Table1)==HTTPService:JSONEncode(Table2)