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

Re^3: Common hash keys

by BrowserUk (Patriarch)
on Jun 08, 2008 at 07:11 UTC ( #690901=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: Common hash keys
by tinita (Parson) on Jun 08, 2008 at 12:11 UTC
    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://690901]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-05-20 16:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (75 votes). Check out past polls.

    Notices?