Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Common hash keys

by lodin (Hermit)
on Jun 08, 2008 at 06:37 UTC ( #690898=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://690898]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-01-23 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?