Confused by Perl grep function
Posted
by titaniumdecoy
on Stack Overflow
See other posts from Stack Overflow
or by titaniumdecoy
Published on 2010-04-15T05:10:28Z
Indexed on
2010/04/15
5:23 UTC
Read the original article
Hit count: 377
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.
© Stack Overflow or respective owner