local a = {a = {1}}
local b = {a = {1}}
if a == b then
print("s")
end
print(a)
print(b)
Why the two table arent same?
I tried to use table.unpack or table.concat.But when When there is another table in one table it will Return nothing.
local a = {a = {1}}
local b = {a = {1}}
if a == b then
print("s")
end
print(a)
print(b)
Why the two table arent same?
I tried to use table.unpack or table.concat.But when When there is another table in one table it will Return nothing.
What you’re doing here is essentially creating two tables. They are different because their memory addresses are different.
To check if two tables share the same content, you can create a for loop like this:
local a = {a = 1}
local b = {a = 1}
local same = false
for i, v in a do
if b[i] == v then
same = true
else
same = false
end
end
print(same)
If you want this solution to also support table values, then you can try the solution below:
local a = {a = {1}}
local b = {a = {1}}
-- A function to check if the given two tables are the same, with recursion
local function recursiveCheck(firstTable, secondTable)
local same = false
for i, v in firstTable do
if secondTable[i] == v then -- If the two values are the same, then set the same variable to true
same = true
elseif typeof(secondTable[i]) == "table" then -- If the first value is a table, then we run this function recursively to check if that table is also the same as the other value
same = recursiveCheck(secondTable[i], v)
else -- If they aren't, set the same variable to false
same = false
end
end
return same -- Returns the same variable's value
end
local isSame = recursiveCheck(a, b)
print(isSame) -- Prints the given value
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.