Experimenting on your own will prove that only one search is needed. You did try this out on your own, didn't you? Anyway, here is a simple example where the common keys are b and c:
#!/usr/bin/env perl
use warnings;
use strict;
my %hash1 = (a=>1,b=>2,c=>3,d=>4);
my %hash2 = ( b=>5,c=>6, e=>7);
my @common = ();
foreach (keys %hash1) {
push(@common, $_) if exists $hash2{$_};
}
# @common now contains common keys
print "common1: @common\n";
@common = (); # empty the array
# reverse the search:
foreach (keys %hash2) {
push(@common, $_) if exists $hash1{$_};
}
print "common2: @common\n";
prints:
common1: c b
common2: c b
|