Harry Putnam wrote:
But, is there an easier way?
Invert both hashes and find the keys in both inverses.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;
my %h1 = (
'./b/f1' => 'f1',
'./b/c/fa' => 'fa',
'./b/l/c/f2' => 'f2',
'./b/g/f/r/fb' => 'fb'
);
my %h2 = (
'./b/fb' => 'fb',
'./b/c/fd' => 'fd',
'./b/l/c/f2' => 'f2',
'./b/g/f/r/fc' => 'fc',
'./b/g/h/r/fb' => 'fb'
);
my %inverse_h1 = invert( \%h1 );
my %inverse_h2 = invert( \%h2 );
# print 'h1: ', Dumper \%h1, \%inverse_h1;
# print 'h2: ', Dumper \%h2, \%inverse_h2;
for my $name ( keys %inverse_h1 ){
if( exists $inverse_h2{$name} ){
print "$name exists in both hashes:\n",
Data::Dumper->Dump( [ $inverse_h1{$name}, $inverse_h2{$name}
], [ 'h1', 'h2' ] ),
"\n";
}
}
sub invert {
my $h = shift @_;
my %inv = ();
while( my ( $k, $v ) = each %{ $h } ){
push @{ $inv{$v} }, $k;
}
return %inv;
}
__END__
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/