Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Comparing 2 hash tables where values are stored in arrays

by AppleFritter (Vicar)
on Sep 05, 2014 at 18:11 UTC ( #1099718=note: print w/replies, xml ) Need Help??


in reply to Comparing 2 hash tables where values are stored in arrays

I'd use List::Compare (though I'm not sure I like its object-oriented interface):

#!/usr/bin/perl use strict; use warnings; use feature qw/say/; use List::Compare; use List::MoreUtils qw/uniq/; my %hash1 = ("abc" => [1, 2, 3, 4]); my %hash2 = ("abc" => [1, 3, 5, 7]); $, = ","; foreach my $key (uniq (keys %hash1, keys %hash2)) { my $lc = List::Compare->new($hash1{$key} // [], $hash2{$key} // [] +); say $lc->get_Lonly; # elements only in $hash1{$key} say $lc->get_Ronly; # elements only in $hash2{$key} }

Replies are listed 'Best First'.
Re^2: Comparing 2 hash tables where values are stored in arrays
by Athanasius (Archbishop) on Sep 06, 2014 at 03:27 UTC
    I'd use List::Compare (though I'm not sure I like its object-oriented interface)

    Then use the same author’s List::Compare::Functional instead  :-)

    #! perl use strict; use warnings; use feature qw/ say /; use List::Compare::Functional qw/ get_unique get_complement /; use List::MoreUtils qw/ uniq /; my %hash1 = (abc => [1, 2, 3, 4]); my %hash2 = (abc => [1, 3, 5, 7]); $, = ','; foreach my $key (uniq(keys %hash1, keys %hash2)) { say get_unique ( [ $hash1{$key} // [], $hash2{$key} // [] ] ); say get_complement( [ $hash1{$key} // [], $hash2{$key} // [] ] ); }

    Output:

    13:23 >perl 1000_SoPW.pl 2,4 5,7 13:23 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Oh, now that's useful! Thanks for the pointer, brother!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-08-19 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?