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

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

by reisinge (Hermit)
on Sep 05, 2014 at 21:08 UTC ( #1099731=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-08-14 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?