|
| 23 Dec 2013 11:02 PM |
Is there any simple way to check if two sets of tables contain the exact same information (even if in a different order)?
eg. {"cat","dog","owl"} and {"dog","owl","cat}
I don't know a lot about metatables so I'm not sure if there is a way to do this through that, or would it require some sort of algorithm?
Any help would be appreciated |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Dec 2013 11:03 PM |
function compare(t1, t2) local len1, lent2 = #t1, #t2 if len1 ~= len2 then return false end local storedKV = {} for i,v in next, t1 do storedKV[i]=v end for i,v in next, t2 do if not storedKV[i] == v then return false end end return true end
I probably messed up, I'm not sure. |
|
|
| Report Abuse |
|
|
wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
|
| 23 Dec 2013 11:06 PM |
I cant do metatables either. There probably is a more efficient way to do this but..
function TablesAreSame(table1, table2) local found = false if not #table1 == #table2 then return false end for i, v in pairs(table1) do found = false for k, q in pairs(table2) do if v==q then found = true break end end if not found then return false end end return true end |
|
|
| Report Abuse |
|
|
wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
|
| 23 Dec 2013 11:06 PM |
| how did you do that in a minute... not fair >:C |
|
|
| Report Abuse |
|
|
|
| 23 Dec 2013 11:10 PM |
Are you looking to see if the tables have identical information, or just if they happen to contain the same information?
That was kind of a weird question, here:
Do you want this
t1 = {1,2,3} t2 = {3,2,1}
to say "yes, these have the same data"
and this
t1 = {1,2,3,4} t2 = {3,2,1}
to say "yes, these also have the same data"
Or do you want it to say "no, these are not the same"?
-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::] |
|
|
| Report Abuse |
|
|
|
| 23 Dec 2013 11:15 PM |
Sorry, the latter; I want to check that they are completely identical, and contain identical information. As in both tables are the exact same but can be in different orders.
I think one of the ones above might have worked though, just trialing
|
|
|
| Report Abuse |
|
|
|
| 23 Dec 2013 11:20 PM |
Here's my take at the problem:
tab1 = {1,2,3} tab2 = {3,2,1}
function compareTables(t1, t2) if (#t1 ~= #t2) then return false end table.sort(t1) table.sort(t2) for i = 1, #t1 do if t1[i] ~= t2[i] then return false end end return true end
-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::] |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Dec 2013 11:20 PM |
| ^ But that won't be good for named-indices |
|
|
| Report Abuse |
|
|
wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
|
| 23 Dec 2013 11:27 PM |
function TablesAreSame(table1, table2) local table3 = table2 local found = false if not #table1 == #table2 then return false end for i, v in pairs(table1) do found = false for k, q in pairs(table3) do if v==q then found = true table.remove(table3, k) break end end if not found then return false end end return true end
i fixed mine so that it would account for things like {1,2,2,3} and {1,3,2,3} |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Dec 2013 11:28 PM |
| ^ table3 is useless, it's just going to remove table 2's keys |
|
|
| Report Abuse |
|
|
|
| 23 Dec 2013 11:33 PM |
| Managed to get it working perfectly, thanks for the help guys. |
|
|
| Report Abuse |
|
|