Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: compare arrays, and list the difference

by Anonymous Monk
on Oct 03, 2012 at 10:51 UTC ( #997027=note: print w/ replies, xml ) Need Help??


in reply to compare arrays, and list the difference

this is quite close but then I'd need to go throught one of the lists one more time :-/

I think you have to :)

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my @source=(1, 2, 3, 4, 5, 7); my @target=(0, 1, 3, 4, 6); my (@union, @intersection, @difference); my %count = (); foreach my $element (@source, @target) { $count{$element}++ } foreach my $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, $e +lement; } dd \@source, \@target; dd \@union; dd \@intersection; dd \@difference; my @values_in_target_not_in_source = do { my %fuf; @fuf{ @union } = undef; delete @fuf{ @target }; keys %fuf; }; my @values_in_source_not_in_target = do { my %fuf; @fuf{ @union } = undef; delete @fuf{ @source }; keys %fuf; }; dd \@values_in_target_not_in_source , \@values_in_source_not_in_target + ; __END__ ([1 .. 5, 7], [0, 1, 3, 4, 6]) [6, 4, 1, 0, 3, 7, 2, 5] [4, 1, 3] [6, 0, 7, 2, 5] ([7, 2, 5], [6, 0])


Comment on Re: compare arrays, and list the difference
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997027]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-07-26 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls