Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: Count non-empty hash values

by pboin (Deacon)
on Oct 22, 2005 at 19:11 UTC ( #502244=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^4: Count non-empty hash values
by Joost (Canon) on Oct 23, 2005 at 00:46 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://502244]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-07-26 05:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found