http://qs321.pair.com?node_id=690898


in reply to Re: Common hash keys
in thread Common hash keys

There's no need to be careful if you handle the 0 case directly:

use List::Util; my $hasInt = defined first { exists $hashRef2->{$_} } keys %$hashRef1 ;

lodin

Replies are listed 'Best First'.
Re^3: Common hash keys
by BrowserUk (Patriarch) on Jun 08, 2008 at 07:11 UTC

    Actually, there's no need to be careful or test for definedness.

    List::Util::first() returns the result of the code block, in this case the boolean return of the exists function. The values associated with the keys never come into it.

    use List::Util qw[ first ];; my $a = { a=>0, b=>0, c=>0 }; my $b = { c=>0, d=>0, e=>0 }; if( first{ exists $b->{ $_ } } keys %$a ) { print "Common keys!"; } Common keys!

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      List::Util::first() returns the result of the code block, in this case the boolean return of the exists function.
      i think it returns the key it's iterating over, just like grep works. from the docs: "first returns the first element where the result from BLOCK is a true value."
      use List::Util qw[ first ]; my $a = { 0 => 0 }; my $b = { 0 => 0 }; if( first{ exists $b->{ $_ } } keys %$a ) { print "Common keys!"; }
      prints nothing.