in reply to Difference arrays.

Since it's you, here is my suggestion:

Update: My "solution" is now shamefully hidden in readmore-tags because it doesn't give you what was asked for. My solution simply gives you (stringified) all elements whch are in the first or the second array but not in both.

use strict; use warnings; use Data::Dumper; my @a = (43,43,44); my @b = (43,43); my @c= difference(\@a, \@b); print Dumper \@c; my @p = ( 1,1,1,1,1,2,2,2,3,3,4,5,6); my @q = ( 1,2,3,4,5,6 ); my @r= difference(\@p, \@q); print Dumper \@r; sub difference { my ($a, $b)= @_; my %d; ++$d{$_} foreach @$a; --$d{$_} foreach @$b; return map { ($_) x abs $d{$_} } keys %d; }

Wouldn't it be you, I would have cried H O M E W O R K! ;-)

Update: Removed an overseen "my $d". Thanks to betterworld


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^2: Difference arrays.
by Anonymous Monk on Sep 04, 2008 at 19:53 UTC
    "Examine what is said, not who speaks"

      "Examine what is said, not who speaks"

      Hey, I think I've seen that in someone's signature! Um, anyway...

      I can't speak for the other monks, but I've found content analysis to be expensive enough to be worth optimizing. As such, it's expedient to apply a simple memoization/caching technique whereby the source of some assertion influences the time and effort I spend evaluating the assertion based on past experiences with that source.

      It's true that a stopped clock is right twice every day, and even kooks can have insights worthy of deep consideration. Still, I don't want to waste my time looking at a clock I already know is broken any more than I want to waste my time pondering the rantings of someone whose rantings I've already pondered at a length greater than their value.

      I'm not talking about anyone in particular here. I just find the "ignore the source" meme a little irritating.

      Heuristics have their place.