Forum: Ruby Compare Arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D420fa0bc1cc612ecf35fea33db361a1?d=identicon&s=25 TJ nonstickglue (superfly)
on 2008-10-12 05:53
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.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-10-12 07:00
(Received via mailing list)
On Sat, Oct 11, 2008 at 10:50 PM, Tj Superfly <nonstickglue@verizon.net>
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 :)

Todd
5725900ad1b504a7dc086742e1103256?d=identicon&s=25 Peda, Allan (NYC-GIS) (Guest)
on 2008-10-12 07:03
(Received via mailing list)
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)
121c13a2b43ffd45bdaa87843605604c?d=identicon&s=25 Christopher Dancy (christoforever)
on 2008-10-12 07:05
(Received via mailing list)
On Oct 11, 11:50 pm, Tj Superfly <nonstickg...@verizon.net> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-12 11:23
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
20228e1a65c9717555f80030a6e8779f?d=identicon&s=25 Rüdiger Brahns (Guest)
on 2008-10-12 16:41
(Received via mailing list)
Todd Benson schrieb:
>>
>
> (a | b) - (a & b)

Why not just

b - a

? But as I understand him what he wants is

a & b
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-10-12 18:07
(Received via mailing list)
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 :-) 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
This topic is locked and can not be replied to.