The arrays are, in a sense, equal because they share the exact same indices with the exact same values. Although, the arrays do have different IDs, so checking for equality using == returns false:
You’d have iterate over both elements and compare the key and values with the other.
-- I don't know if this works
array1 = {a = 100,b=20}
array2 = {a = 100,b=20}
function compare(array1, array2)
for i,v in pairs(array1) do
if v ~= array2[i] then
return false
end
end
return true
end
-- Should return True if arrays are identical & false if unique (different)
print(compare(array1,array2))
While the posted solution does in-fact work, it’s not efficient. True recursion doesn’t involve sub-method calls. This should work with both arrays that were mentioned and shouldn’t have any edge cases.
array1 = {{1,20},{6,10},100}
array2 = {{1,20},{6,10},100}
isEqual = true
function compare(array1,array2)
for i,v in pairs(array1) do
if type(v) == "table" then
compare(v,array2[i])
else
if v ~= array2[i] then
isEqual = false
end
end
end
end
compare(array1,array2)
-- Should return True if arrays are identical & false if unique (different)
print(isEqual)
Not sure how performant this would be, but you could also convert both tables to JSON strings and compare the strings directly to see if they are the same.
isEqual = true
function compare(array1,array2)
if #array1 > #array2 or #array2 > #array1 then isEqual = false end
for i,v in pairs(array1) do
if type(v) == "table" and type(array2[i]) == "table" then
compare(v,array2[i])
else
if v ~= array2[i] then
isEqual = false
end
end
end
end
-- Any table, nest or not works..
compare({{}},{{4}}) -- false
compare({{1}},{1}) -- false
compare({{1}},{{1}}) -- true
Edit: Made this loads more efficient
function compare(array1,array2)
if #array1 > #array2 or #array2 > #array1 then return false end
for i,v in pairs(array1) do
if type(v) == "table" and type(array2[i]) == "table" then
return compare(v,array2[i])
else
if v ~= array2[i] then
return false
end
end
end
return true
end
print(compare({1,{1,2,{1}}},{1,{1,2,{0}}})) -- false
print(compare({1,{1,2,{1}}},{1,{1,2,{1}}})) -- true
local HttpService = game:GetService("HttpService")
local array1 = HttpService:JSONEncode({1,2,3})
local array2 = HttpService:JSONEncode({1,2})
-- True if same, false if unique
print(data1 == data2)
While this is certainly easier, I’m not sure if it’s worth the possible overhead in terms of speed. I’d imagine that importing the HttpService library and calling two functions with their contents arbitrary might cause a performance drop. However, if this library is C-based and ran in C, it’s probably faster.
I’m not entirely sure as I haven’t tested it myself.
Here’s a simple and efficient function that supports nested tables.
local array1 = {100, 2}
local array2 = {100, 1}
local function compare(arr1, arr2)
for i, v in pairs(arr1) do
if (typeof(v) == "table") then
if (compare(arr2[i], v) == false) then
return false
end
else
if (v ~= arr2[i]) then
return false
end
end
end
return true
end
print(compare(array1, array2))
What do you mean by this about “importing” and “library”? Does the engine do this and do you know this for a fact? Like I don’t really understand this bit: what’s this about importing and libraries?
I’m lost as to what you mean since both terms are commonly used in programming. From my understanding, game:GetService() returns an instance of the service name if not already instanced beforehand.
The instance returned acts & functions like ModuleScripts so, if I had to take an educated guess, it would be comparable (albeit not the exact same) to require(). In programming, speed & efficiency is everything. To create an instance, call a method, have the method run arbitrary code and then return a valid result seems to be slower than recursion. However, this is speculation and I haven’t had the free time to write tests to see if this is true.
I mean what I said: what are you talking about when you refer to libraries and importing of them in this situation and do you have a citation that either is involved? I’m asking for curiosity’s sake here.
Good time to remind you that you’re in a place where a majority of developers don’t understand or know these supposedly “common programming terms” as Roblox abstracts away and simplifies mostly everything. Fundamental concepts like libraries and such in other programming are not as standard here.
I don’t think, or wouldn’t like to think, that a library is imported every time you want to run something, doesn’t make sense. At the very least if such a process was involved, it would be available somewhere in some resource repository and a call would be memoised (cached) so that said expense is fairly low. The only real weight that would be carried here is converting the table into a string format and I assume that’s fairly negligible.
With HttpService, the service already exists at runtime, so the GetService call would only fetch its name. That leaves it down to fetching the service and calling encode twice, then comparing. A variable remains though which is in what language the encoding is being done in.
That aside though, for this specific case, I would probably stick with traversing both tables. How expensive both are I don’t know and whether or not its negligible is in the air too, but I am almost certain it is. Up to testing to find out.
Worth noting that LuaU improved iterating by a large amount as well as calls to methods.
Another way to compare the two arrays would be using HttpService.
local httpService = game:GetService("HttpService")
local array1 = {1, 2, {3}}
local array2 = {1, 3, {2}}
local function compare(arr1, arr2)
local arr1s = httpService:JSONEncode(arr1)
local arr2s = httpService:JSONEncode(arr2)
return (arr1s == arr2s)
end
print(compare(array1, array2))
This was suggested earlier and discouraged (?), since it would be better to compare the contents of the tables themselves instead of converting them to another format and comparing then.