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
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!