From: "David A. Black" Date: 2008-10-13T01:03:33+09:00 Subject: Re: Compare Arrays --1926193751-810982787-1223827571=:29619 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-810982787-1223827571=:29619" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-810982787-1223827571=:29619 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sun, 12 Oct 2008, R=FCdiger Brahns wrote: > Todd Benson schrieb: >> On Sat, Oct 11, 2008 at 10:50 PM, Tj Superfly = =20 >> wrote: >>> Hey, I just have a quick question about arrays. >>>=20 >>> I have two different ones that I would like to compare. >>>=20 >>> Example: >>>=20 >>> Array 1: cats, dogs, monkeys >>>=20 >>> Array 2: cats, fish, dogs, birds, monkeys >>>=20 >>> 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. >>>=20 >>> So what the program should output is fish, monkeys. >>=20 >> Hmm. I'm assuming you are looking for ["fish", "birds"], and if you >> are, do union, then subtract intersection... >>=20 >> a =3D %w( cats dogs monkeys ) >> b =3D %w( cats fish dogs birds monkeys ) >>=20 >> (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 =3D [1,2,3,4,5].extend(EnumExOr) p array ^ [3,4,5,6,7] # [1,2,6,7] David --=20 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! --1926193751-810982787-1223827571=:29619-- --1926193751-810982787-1223827571=:29619--