Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^2: Count non-empty hash values

by Joost (Canon)
on Oct 22, 2005 at 17:47 UTC ( #502235=note: print w/replies, xml ) Need Help??

in reply to Re: Count non-empty hash values
in thread Count non-empty hash values

Or even
my $non_empty = grep { $_ ne "" } values %a;
which is a little clearer IMHO, and probably a tiny bit faster as well :-)

Replies are listed 'Best First'.
Re^3: Count non-empty hash values
by fishbot_v2 (Chaplain) on Oct 22, 2005 at 17:58 UTC
    and probably a tiny bit faster

    Yep, you've got me there, and then some:

    Rate OP other fishbot Joost OP 293/s -- -21% -76% -92% other 369/s 26% -- -69% -90% fishbot 1205/s 312% 226% -- -68% Joost 3723/s 1173% 908% 209% --

    "other" was a solution using values with a regex. "OP" was a corrected key and regex version of the OP's code.

    Update: Added OP-style solution to benchmark.

      grep length, values %a
      is even faster.
      Rate OP other fishbot Joost ikegami OP 186/s -- -34% -67% -88% -90% other 280/s 51% -- -51% -82% -84% fishbot 571/s 207% 104% -- -64% -68% Joost 1578/s 750% 463% 176% -- -12% ikegami 1798/s 868% 542% 215% 14% --

      Whenever posting a benchmark, it is best to include the exact script code you used to generate the benchmark, so that others are able to see that you implemented each benchmark case properly and in its most efficient form. I have seen many cases where the original benchmark results provided by a monk have been completely bogus because of implementation problems in one or more of the benchmark cases. There is even the odd node where the result representing the best benchmark case is incorrect, and where that "best benchmark case" actually comes out as the worst case you could possibly use in real code.

Re^3: Count non-empty hash values
by pboin (Deacon) on Oct 22, 2005 at 19:11 UTC

    Great answers fishbot_v2 and Joost.

    I figured out what threw me: In perldoc -f grep the code examples all use regex. I was mistaken when I thought grep just did the equivalent of "$_ =~ /myregex/". It does more than that. Even Programming Perl examples are of the regex type. For any future readers, I found a better example of the inequality usage (and some others) in Perl Cookbook, recipie 4.13.

    Also, kudos to Joost for using values(). I'm so used to calling by key with the arrow operator, that I comletely forgot that such a function existed.

      grep just localizes the "current" item in the list to $_ - that's why /regex/ works in grep (and map and for(each) too): you can do any operation you want on $_ and as long as the statement(s) in the grep block return(s) a true value the original value will be in the resulting list (or in the count, if in scalar context).

      I guess this is one of the points where perl is intuitive and "deep" at the same time.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2020-12-01 09:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found