Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Think about Loose Coupling
 
PerlMonks  

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

by dirtdog (Beadle)
on Jun 05, 2010 at 17:31 UTC ( #843282=note: print w/ replies, xml ) Need Help??


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

I see the problem now

I'm expecting grep to return true or false, but since this is a list context it will return the actual values. 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?


Comment on Re^2: Trouble grepping values when hash contains multiple values per key
Re^3: Trouble grepping values when hash contains multiple values per key
by toolic (Chancellor) on Jun 05, 2010 at 17:38 UTC
    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?
    Something like:
    my @date = grep ... if (@date) { ... }
Re^3: Trouble grepping values when hash contains multiple values per key
by dirtdog (Beadle) on Jun 05, 2010 at 17:39 UTC

    I just changed the until loop to a while loop and now it's working as expected

    Sorry for wasting your time...I was not thinking clearly

    while (my @date = grep /$new_ex_date/,@{$countries{$cntry_of_issue}}) { $new_date=get_next_bus_day($new_date); }
Re^3: Trouble grepping values when hash contains multiple values per key
by chromatic (Archbishop) on Jun 05, 2010 at 19:47 UTC
    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.

      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
Node Status?
node history
Node Type: note [id://843282]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-04-16 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (417 votes), past polls