generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Comparing tables

Previous Thread :: Next Thread 
0supadude0 is not online. 0supadude0
Joined: 11 Aug 2010
Total Posts: 649
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 is not online. 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 is not online. 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 is not online. 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
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
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
0supadude0 is not online. 0supadude0
Joined: 11 Aug 2010
Total Posts: 649
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
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
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 is not online. 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 is not online. 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 is not online. 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
0supadude0 is not online. 0supadude0
Joined: 11 Aug 2010
Total Posts: 649
23 Dec 2013 11:33 PM
Managed to get it working perfectly, thanks for the help guys.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image