Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Trouble grepping values when hash contains multiple values per key

by chromatic (Archbishop)
on Jun 05, 2010 at 19:47 UTC ( [id://843288]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Trouble grepping values when hash contains multiple values per key
in thread Trouble grepping values when hash contains multiple values per key

Is there any way to force grep to evaluate these in a scalar context in order to return a "true" or "false" in this case?

Yes, evaluate grep in scalar context. It's that easy!

my $found_any = grep { ... } @whatever;

Boolean context is also a scalar context.

  • Comment on Re^3: Trouble grepping values when hash contains multiple values per key
  • Download Code

Replies are listed 'Best First'.
Re^4: Trouble grepping values when hash contains multiple values per key
by eyepopslikeamosquito (Archbishop) on Jun 06, 2010 at 03:13 UTC

    my $found_any = grep { ... } @whatever;
    Because grep always iterates through the entire list, to stop on the first one found, you might try instead:
    use List::MoreUtils qw(any); my $found_any = any { ... } @whatever;
    or:
    use List::Util qw(first); my $found_any = defined first { ... } @whatever;
    ...though, in practice, it is unlikely to matter, performance-wise, unless @whatever is huge.

    As an aside, note that both any and first are built in to Perl 6.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found