Compare Arrays

T

Tj Superfly

Hey, I just have a quick question about arrays.

I have two different ones that I would like to compare.

Example:

Array 1: cats, dogs, monkeys

Array 2: cats, fish, dogs, birds, monkeys

Now, I would like to compare array 2 with array 1 and have it spit out
any words that aren't in both... if that makes sense.

So what the program should output is fish, monkeys.
 
T

Todd Benson

Hey, I just have a quick question about arrays.

I have two different ones that I would like to compare.

Example:

Array 1: cats, dogs, monkeys

Array 2: cats, fish, dogs, birds, monkeys

Now, I would like to compare array 2 with array 1 and have it spit out
any words that aren't in both... if that makes sense.

So what the program should output is fish, monkeys.

Hmm. I'm assuming you are looking for ["fish", "birds"], and if you
are, do union, then subtract intersection...

a = %w( cats dogs monkeys )
b = %w( cats fish dogs birds monkeys )

(a | b) - (a & b)

More than two arrays is left as an exercise :)

Todd
 
P

Peda, Allan (NYC-GIS)

You are doing set operations, so I looked there first:
require 'set'
s1 =3D Set.new [1, 2, 7, 8, 9, 45, 98, 85, 39] =20
s2 =3D Set.new [1, 45, 66, 4, 84, 98 ]

p (s1 - s2) | (s2 - s1)



-----Original Message-----
From: Tj Superfly [mailto:[email protected]]
Sent: Sat 10/11/2008 11:50 PM
To: ruby-talk ML
Subject: Compare Arrays
=20
Hey, I just have a quick question about arrays.

I have two different ones that I would like to compare.

Example:

Array 1: cats, dogs, monkeys

Array 2: cats, fish, dogs, birds, monkeys

Now, I would like to compare array 2 with array 1 and have it spit out
any words that aren't in both... if that makes sense.

So what the program should output is fish, monkeys.
--=20
Posted via http://www.ruby-forum.com/.




This message contains information which may be confidential and privileged.
Unless you are the intended recipient (or authorized to receive this message
for the intended recipient), you may not use, copy, disseminate or disclose=
to
anyone the message or any information contained in the message. If you have
received the message in error, please advise the sender by reply e-mail, and
delete the message. Thank you very much.
(A)
 
C

christoforever

Hey, I just have a quick question about arrays.

I have two different ones that I would like to compare.

Example:

Array 1: cats, dogs, monkeys

Array 2: cats, fish, dogs, birds, monkeys

Now, I would like to compare array 2 with array 1 and have it spit out
any words that aren't in both... if that makes sense.

So what the program should output is fish, monkeys.

Im assuming the elements in the array are strings so.....

so we have arr1 and arr2 with the previous mentioned elements i guess
you could make a method something like this and then depending on how
your comparing these elements...

def print_elements_not_in_array
arr1.each do |temp|
arr2.each do |temp2|
puts " #{temp2} " if temp1 != temp2
end
end
end
 
B

Brian Candler

Tj said:
Hey, I just have a quick question about arrays.

I have two different ones that I would like to compare.

Example:

Array 1: cats, dogs, monkeys

Array 2: cats, fish, dogs, birds, monkeys

Now, I would like to compare array 2 with array 1 and have it spit out
any words that aren't in both... if that makes sense.

So what the program should output is fish, monkeys.

If arr2 is always a superset of arr1, then arr2-arr1
 
R

Rüdiger Brahns

Todd said:
Hey, I just have a quick question about arrays.

I have two different ones that I would like to compare.

Example:

Array 1: cats, dogs, monkeys

Array 2: cats, fish, dogs, birds, monkeys

Now, I would like to compare array 2 with array 1 and have it spit out
any words that aren't in both... if that makes sense.

So what the program should output is fish, monkeys.

Hmm. I'm assuming you are looking for ["fish", "birds"], and if you
are, do union, then subtract intersection...

a = %w( cats dogs monkeys )
b = %w( cats fish dogs birds monkeys )

(a | b) - (a & b)

Why not just

b - a

? But as I understand him what he wants is

a & b
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top