hello,
how would i check if 2 tables have the same things inside them if they’re not in the same order?
for example:
local table1 = {"hi","140","bro"}
local table2 = {"140","hi","bro"}
^ those would be the same
local table1 = {"hi","140","140"}
local table2 = {"140","bro","bro"}
^ and those wouldn't
local function testSame(one, two)
for i,v in pairs(one) do
if not table.find(two, v) then
return false
end
end
for i,v in pairs(two) do
if not table.find(one, v) then
return false
end
end
return true
end
local table1 = {"hi","140","bro"}
local table2 = {"140","hi","bro"}
local TableStatus = true
for I, V in pairs(table1)do
local Matches = 0
for II, VV in pairs (table2)do
if TableStatus == false then
break
end
if V == VV then
Matches += 1
end
end
if TableStatus == false then
break
end
if Matches >= 1 then
continue
else
TableStatus = false
end
end
if TableStatus then
print("they are the same")
else
print("they aint")
end
well your wrong because as u can see if i run the code above (with the tables not in the same order) the outpuit is correct, output:
run it yourself if you want
local table1 = {"IM","IN","THE", "WRONG", "ORDER"}
local table2 = {"ORDER","WRONG","THE","IN","IM"}
local TableStatus = true
for I, V in pairs(table1)do
local Matches = 0
for II, VV in pairs (table2)do
if TableStatus == false then
break
end
if V == VV then
Matches += 1
end
end
if TableStatus == false then
break
end
if Matches >= 1 then
continue
else
TableStatus = false
end
end
if TableStatus then
print("they are the same")
else
print("they aint")
end
Oh, I appear to have been mistaken. Sorry for my misunderstaning. Although, @pastelpieeee, I do think that there is a problem with both answers that you should keep in mind. If, for instance, “hi” appears twice in one of the tables but only once in the other, it will still say they match.
i present the mightiest table identical identifier lol @pastelpieeee
if like @GamingBroCoolkid328 said you want it to check if they are EXACTLY identical this code works
local table1 = {"IM","IN","THE", "WRONG", "ORDER"}
local table2 = {"ORDER","WRONG","THE","IN","IM"}
local TableStatus = true
for I, V in pairs(table1)do
local Matches = 0
local ExpectedMatches = 0
for III, VVV in pairs(table1)do
if V == VVV then
ExpectedMatches += 1
end
end
for II, VV in pairs (table2)do
if TableStatus == false then
break
end
if V == VV then
Matches += 1
end
end
if TableStatus == false then
break
end
if Matches == ExpectedMatches then
continue
else
TableStatus = false
end
end
if TableStatus then
print("they are the same")
else
print("they aint")
end
A more efficient, less convoluted and straight to the point, clear solution in case anyone has trouble understanding the marked solution.
A summarization of what I’m doing here:
loop through every element in table1
check if each value is found in the second table table2
if found, remove that value so that it is not counted again when checking whether a value exists in the second table
when found, set a variable same to false to indicate the tables are not equal
local table1 = {"hi","140","bro"}
local table2 = {"140","bro","hi"}
local function isEqual(t1, t2)
--// the below is just something I do, not needed with slight modification
--// calling from the table library is likely faster
setmetatable(t2, {__index=table});
local same = false;
if(#t1==#t2) then
same = true
for _, v in ipairs(t1) do
if (not t2:find(v)) then
same = false
break;
else
--// found, so remove this now to avoid having found a value at the same index in the case of multiple values
t2:remove(t2:find(v))
end
end
end
return same
end
local areEqual = isEqual(table1, table2)
print("table 1 and 2 are "..(areEqual and "same" or "different"))
Here is a short and simple solution for people who want it:
local function isTabEqual(tab1, tab2)
if typeof(tab1) ~= typeof(tab2) then return end
for i, v in pairs(tab1) do
if typeof(v) == "table" then
if not isTabEqual(v, tab2[i]) then return end
else
if tab2[i] ~= v then return end
end
end
return true
end