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 » Scripters
Home Search
 

Re: Compare Tables

Previous Thread :: Next Thread 
ColdDeveloper is online. ColdDeveloper
Joined: 25 Jan 2010
Total Posts: 3344
16 Mar 2017 09:04 PM
If I had two tables such as

local Tb1 = {"Blue", "Blue", "Green", "Green"}
local Tb2 = {"Green", "Blue", "Green", "Blue"}

Although its a different order, it contains the same words/characters. How can I make it return true if the strings inside Tb2 are the same as Tb1, regardless of the order. But for example if Tb2 was: local Tb2 = {"Red", "Green", "Blue", "Blue"} then it would return with false.


Report Abuse
ColdDeveloper is online. ColdDeveloper
Joined: 25 Jan 2010
Total Posts: 3344
16 Mar 2017 10:06 PM
b1


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
16 Mar 2017 10:12 PM
set both of their metatables to a table that has an __eq that creates a dictionary of how many times each value occured in the first one then iterate over the second one and subtract the ones that you mapped when they occurred the first iteration and at the end if next(tbl) is not nil or in the second iteration ####### is nil then return false since not everything in 2nd was in first in that case.
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
16 Mar 2017 10:17 PM
Are the tables multi-dimensional or are they only occupied with non-table values?


Report Abuse
ColdDeveloper is online. ColdDeveloper
Joined: 25 Jan 2010
Total Posts: 3344
16 Mar 2017 11:09 PM
They are both non-table values (assuming that means string values)


Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
16 Mar 2017 11:12 PM
then use cntkillme's solution


Report Abuse
ColdDeveloper is online. ColdDeveloper
Joined: 25 Jan 2010
Total Posts: 3344
16 Mar 2017 11:15 PM
Would you be able to make an example? I don't quite understand.


Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
16 Mar 2017 11:16 PM
What don't you understand?

If it's the metatable part, then implementing that isn't required. It's just for simplicity so you don't need to explicitly call a function each time.


Report Abuse
ColdDeveloper is online. ColdDeveloper
Joined: 25 Jan 2010
Total Posts: 3344
16 Mar 2017 11:17 PM
I'm not sure how to add it, I'm looking at the wiki currently but its not really something I have used before.


Report Abuse
PhoenixSigns is not online. PhoenixSigns
Joined: 17 Sep 2011
Total Posts: 1137
16 Mar 2017 11:19 PM
Basically create a new table.

local newTab = {}

then iterate through the first table...
for i = 1,#Tb1 do
if newTab[Tb1[i]] then
newTab[Tb1[i]] = newTab[Tb1[i]] + 1
else
newTab[Tb1[i]] = 1
end
end

Then iterate through the second table... you can do that code yourself. Not going to write the whole thing for you:)
Report Abuse
Lecturous is not online. Lecturous
Joined: 17 Aug 2013
Total Posts: 1096
16 Mar 2017 11:50 PM
local Tb1 = {"Blue", "Blue", "Green", "Green"}
local Tb2 = {"Green", "Blue", "Green", "Blue"}

table.sort(Tb1); table.sort(Tb2)
print(unpack(Tb1) == unpack(Tb2))

Is this what you wanted?
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
16 Mar 2017 11:53 PM
"Is this what you wanted?"

Most definitely not. You're only comparing the first return value of unpack() called on each array.


Report Abuse
Lecturous is not online. Lecturous
Joined: 17 Aug 2013
Total Posts: 1096
16 Mar 2017 11:58 PM
oops, sorry. Ignoring that print statement, just iterate over one of the tables and for each value, check if the corresponding value in the other table is equal. Did I get something wrong?
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
16 Mar 2017 11:59 PM
Did you read the OP? Each value in both tables don't need to have corresponding indices. See cntkillme's reply.


Report Abuse
sayhisam1 is not online. sayhisam1
Joined: 25 Nov 2009
Total Posts: 2092
17 Mar 2017 12:00 AM
How many times are you comparing the two tables?

If you are doing this a lot, it would probably be more efficient to sort both tables first, and then go from the left values. Or make a new table(let's call it newTable), and for every element in table 1, set newTable[a[i]] = true. Then go through the second table and check if newTable[b[i]] = true, for all elements in the second table. That's O(m+n)
Report Abuse
Lecturous is not online. Lecturous
Joined: 17 Aug 2013
Total Posts: 1096
17 Mar 2017 12:10 AM
"local Tb1 = {"Blue", "Blue", "Green", "Green"}
local Tb2 = {"Green", "Blue", "Green", "Blue"}

table.sort(Tb1); table.sort(Tb2)"

"Ignoring that print statement, just iterate over one of the tables and for each value, check if the corresponding value in the other table is equal."

IIRC if the table are sorted, the tables should have corresponding indicies. This is how I'm seeing it:

> tables are sorted
Tb1/Tb2
-----------
Blue == Blue
Blue == Blue
Green == Green
Green == Green
>returns true

but if Tb1 = {"Blue","Red","Green","Green"} then

> tables are sorted
Tb1/Tb2
-----------
Blue == Blue
Green ~= Blue
>returns false

what's wrong with it?
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
17 Mar 2017 12:15 AM
I'm beginning to think that you don't even understand how Lua works. Run this code for me:

local tab = {1, 2, 3}
local var = unpack(tab)
print(var)

It will print the number 1. The unpack() function returns multiple values, and running something like this:

local tab = {1, 2, 3}
local tab2 = {1, 5, 7}
print(unpack(tab) == unpack(tab2))

...will return `true` because it's only comparing the first return value of the unpack() call on each table.


Report Abuse
Lecturous is not online. Lecturous
Joined: 17 Aug 2013
Total Posts: 1096
17 Mar 2017 12:23 AM
I'm beginning to think you don't understand what I'm saying.

"Ignoring that print statement, just iterate over one of the tables and for each value, check if the corresponding value in the other table is equal."

Do you need me to rephrase it?

"Ignoring that unpack was in the script at all, just iterate over one of the tables and for each value, check if the corresponding value in the other table is equal."

After they sort the table, they do what I said in the sentence above. Unpack isn't in my example anymore. Does that clear it up for you?
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
17 Mar 2017 12:31 AM
There you go, you finally got it (kind of).

At the end of the iteration you would have to make sure both tables have the same length, and that function would only work for arrays without holes.


Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
17 Mar 2017 12:32 AM
"At the end"

Well or the beginning


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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