Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comparing 2 hash tables where values are stored in arrays

by jumpingmonkey (Novice)
on Sep 05, 2014 at 18:03 UTC ( [id://1099717]=perlquestion: print w/replies, xml ) Need Help??

jumpingmonkey has asked for the wisdom of the Perl Monks concerning the following question:

How do I compare 2 hash tables where values are stored in arrays in each hash? eg
Hash 1 Key = abc Value = (1,2,3,4) Hash 2 Key = abc Value = (1,3)
Report what is missing in Hash 1 or 2. Short code is preferred :)

Replies are listed 'Best First'.
Re: Comparing 2 hash tables where values are stored in arrays
by AppleFritter (Vicar) on Sep 05, 2014 at 18:11 UTC

    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} }
      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!
Re: Comparing 2 hash tables where values are stored in arrays
by reisinge (Hermit) on Sep 05, 2014 at 21:08 UTC

    I think you basically want to compare two arrays which you get by de-referencing values for each key:

    #!/usr/bin/perl use strict; use warnings; my %hash1 = ( abc => [ 1, 2, 3, 4 ], cde => [ 4, 5, 6 ] ); my %hash2 = ( abc => [ 1, 3 ], cde => [ 4, 5 ] ); my @hash1only; for my $key ( keys %hash1 ) { my @array1 = @{ $hash1{$key} }; my @array2 = @{ $hash2{$key} }; # build lookup table my %seen; foreach my $element (@array2) { $seen{$element} = 1 } # find only elements in @array1 and not in @array2 foreach my $element (@array1) { unless ( $seen{$element} ) { push @hash1only, $element; } } } @hash1only = sort @hash1only; print "Elements in \%hash1 and not in \%hash2: @hash1only\n";
    See recipe 4.8 in Perl Cookbook for more.

    And like that ... he's gone. -- Verbal

Re: Comparing 2 hash tables where values are stored in arrays
by Laurent_R (Canon) on Sep 05, 2014 at 21:31 UTC
    The module suggested by AppleFritter is certainly the easiest way.

    But in case you want to do it yourself in pure Perl without using a module, the most idiomatic way to do it is probably to use a hash (or two hashes in this case). This command-line script (re-using AppleFritter sample data) should give you a gist on how to do it:

    $ perl -E ' my %hash1 = ("abc" => [1, 2, 3, 4]); my %hash2 = ("abc" => [1, 3, 5, 7]); my %val1 = map {$_, 1} @{$hash1{abc}}; my %val2 = map {$_, 1} @{$hash2{abc}}; say "Values missing in hash2: ", join " ", grep {not exists $val2{$_}} + keys %val1; say "Values missing in hash1: ", join " ", grep {not exists $val1{$_}} + keys %val2; ' Values missing in hash2: 4 2 Values missing in hash1: 7 5
    Please ask if there is something you don't understand.

    Edit: I had not seen j0se's solution when I suggested mine. Although the two solutions look very different, they are based on exactly the same ideas. I probably would not have posted anything if I had seen j0se's post before. Now it is done, I leave the post, it shows at least that, really, TIMTOWTDI in Perl.

    (BTW, j0se, I have spent at least 18 weeks in your country, specifically in Bratislava, working for Orange Slovakia back in 2008 (preparing the Euro transition), and also 6 or 7 more weeks a few years before. And, long before, I also visited Bratislava as a young tourist back in 1975, in a totally different and quite difficult political context. Nice city and very nice people. Sorry for the off-topic digression.)

Re: Comparing 2 hash tables where values are stored in arrays
by linuxer (Curate) on Sep 05, 2014 at 21:02 UTC
Re: Comparing 2 hash tables where values are stored in arrays
by Solo (Deacon) on Sep 05, 2014 at 18:12 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-03-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found