Vaishnavi Saba wrote:
Hi,

Hello,

My query: In the code shown below, How does *grep* compare a scalar( *
$seen{$_}* ) with an array of hash references ( *...@_* ).

An array in scalar context returns the number of elements in the array and @_ (which is an alias to @common) contains 3 elements so:

return grep { $seen{$_} == @_ } keys %seen;

is the same as:

return grep { $seen{$_} == 3 } keys %seen;

and in list context grep returns a list of all the keys of %seen where the value of that key is 3 and in scalar context grep returns a count of the number of keys that match.

=====

@common = inter( \%foo, \%bar, \%joe );
sub inter {
     my %seen;
     for my $href (@_) {
         while (my $k = each %$href ) {
             $seen{$k}++;
         }
     }

return grep { $seen{$_} == @_ } keys %seen;

}

========

When I try printing the @_, I get the output as "HASH(0x40030354)
HASH(0x400303d8) HASH(0x40037e98)".

That is because @_ (which is an alias to @common) contains three hash references.


John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to