Confused by Perl grep function
- by titaniumdecoy
I don't understand the last line of this function from Programming Perl 3e.
Here's how you might write a function that does a kind of set intersection by returning a list of keys occurring in all the hashes passed to it:
@common = inter( \%foo, \%bar, \%joe );
sub inter {
my %seen;
for my $href (@_) {
while (my $k = each %$href) {
$seen{$k}++;
}
}
return grep { $seen{$_} == @_ } keys %seen;
}
I understand that %seen is a hash which maps each key to the number of times it was encountered in any of the hashes provided to the function.