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.

On Sat, Oct 11, 2008 at 10:50 PM, Tj Superfly [email protected]
wrote:

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

Todd

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)

On Oct 11, 11:50 pm, Tj Superfly [email protected] wrote:

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

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

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

Todd B. schrieb:

(a | b) - (a & b)

Why not just

b - a

? But as I understand him what he wants is

a & b

Hi –

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

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