Compare Arrays

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.

···

--
Posted via http://www.ruby-forum.com/.

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 :slight_smile:

Todd

···

On Sat, Oct 11, 2008 at 10:50 PM, Tj Superfly <nonstickglue@verizon.net> wrote:

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.

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

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

···

-----Original Message-----
From: Tj Superfly [mailto:nonstickglue@verizon.net]
Sent: Sat 10/11/2008 11:50 PM
To: ruby-talk ML
Subject: Compare Arrays

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.
--
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)

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

···

On Oct 11, 11:50 pm, Tj Superfly <nonstickg...@verizon.net> wrote:

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.
--
Posted viahttp://www.ruby-forum.com/.

Tj Superfly wrote:

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

···

--
Posted via http://www.ruby-forum.com/\.

Todd Benson schrieb:

···

On Sat, Oct 11, 2008 at 10:50 PM, Tj Superfly <nonstickglue@verizon.net> wrote:

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

Hi --

Todd Benson schrieb:

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

As Brian C. said, that works but only if b is always a superset of a.
If not, you get:

   [1,2,3] - [2,3,4] # [1], should be [1,4]

? But as I understand him what he wants is

a & b

I think he wants:

   a ^ b # exclusive or

which unfortunately doesn't exist :slight_smile: It's basically

   (a | b) - (a & b)

which I think someone else posted. Or, if you don't want to create
that many intermediate arrays, you could do:

module EnumExOr
   def ^(other)
     reject {|e| other.include?(e) }.
     concat(other.reject {|e| include?(e) })
   end
end

array = [1,2,3,4,5].extend(EnumExOr)

p array ^ [3,4,5,6,7] # [1,2,6,7]

David

···

On Sun, 12 Oct 2008, Rüdiger Brahns wrote:

On Sat, Oct 11, 2008 at 10:50 PM, Tj Superfly <nonstickglue@verizon.net> >> wrote:

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!