Im trying to make it so I can compare if all the items from one table are in another, in no particular order.
local table1 = {item1, item2, item3}
local table2 = {item1, item2}
--These tables would be equal even though table1 has an extra item, because they both have all the items in table 2
local table1 = {item1, item2, item3}
local table2 = {item1, item4}
--these tables wouldnt be even because table 1 doesnt have everything in table 2
Ive seen multiple things on dev forum, but they’re all just at least 2 match or something. Ive tried making it but I dont think im good enough at coding to make it.
I made function that allows you to get matched values
heres the code:
local T1 = {"Apple","Car","Sowrd","Roblox"}
local T2 = {"Roblox","Car","Plane","Tower"}
function gmatch_table(table1,table2)
local Matched = {} -- goal: {"Roblox","Car"}
local PriorityTable
local MatchingTable
if #table1 < #table2 then -- i use this method becuase it's the only one i can think of atm :)
PriorityTable = table1
MatchingTable = table2
else
PriorityTable = table2
MatchingTable = table1
end
for i,TableValue1 in pairs(PriorityTable) do
for i,TableValue2 in pairs(MatchingTable) do
if TableValue1 == TableValue2 then
table.insert(Matched,{TableValue1,TableValue2})
end
end
end
return Matched
end
print(gmatch_table(T1,T2))
You can get only one value instead of nested tables by changing this:
local function equal(t1: {}, t2: {}): boolean
for _, v in pairs(t2) do
if not table.find(t1, v) then return false end
end
return true
end
local function equalNoOrder(t1: {}, t2: {}): boolean
return equal(t1, t2) or equal(t2, t1)
end
This is the output for Matched that I got, Im not sure what the problem is. There is multiple of the same value in Table 1, Hopefully this doesnt matter as it kind of matters in my code.
This should be exactly if you want, if Table1 has the items in Table2, then its matching. If Table 1 has the same items as Table2, but table 1 has an extra item, then its still matching because it has the items in the table 2 at least. If Table 2 has a value that table 1 doesn’t, then its not matching.
Example:
Table 1 has item1, item2, item3, item4
Table 2 has item1, item2, item3
They still match because Table 1 has all of the items in table 2. But if table 1 didnt have item 3, and it just had 1, 2, 4, it doesnt match because it doesnt have the 3.
CODE
local table1 = {"A", "B", "C", "D"} --table 1 is equal to table 2 because it has A & B
local table2 = {"A", "B", "D"}
local matching = {}
local equal = false --Sets false to begin.
for i, olditem in pairs(table1) do
for i, item in pairs(table2) do
if olditem == item then
table.insert(matching, olditem)
end
end
end
if #matching == #table2 then
equal = true
print("Table 1 does match table 2.")
else
equal = false --use the equal variable for what ever you want.
print("Table 1 does not match table 2.")
end
This sort of worked, I should’ve clarified before I posted that there might be multiple of the same value in both tables.
local table1 = {"A", "B", "C", "D"}
local table2 = {"A", "B", "D", "D"}
--These would not be equal because table1 only has one 'D' while table 2 has 2.
local table1 = {"A", "B", "C", "D", "D", "D"}
local table2 = {"A", "B", "D", "D"}
--These would be equal because table1 has at least as much as table2.
You can use the code provided by NyrionDev but edit it slightly:
local function equal(t1: {}, t2: {}): boolean
local localTable = t1
for _, v in pairs(t2) do
if not table.find(localTable, v) then return false end
table.remove(localTable, table.find(localTablel, v))
end
return true
end
local function equalNoOrder(t1: {}, t2: {}): boolean
return equal(t1, t2) or equal(t2, t1)
end