good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Increasing key/values held in a Hash from an Arrayby xdg (Monsignor) |
on Mar 17, 2006 at 21:33 UTC ( [id://537588]=note: print w/replies, xml ) | Need Help?? |
I'd reverse the nature of the hash and store lines as keys and values as the count.
Gives:
(I'm sure this can be golfed, but I think it's clearer this way.) Update: reading ikegami's answer, we're both on the right track, but I think solving slightly different problems. I'm not sure which one is the one you want: a count of occurances of keys across all lines or a count of occurances of keys within lines. And do you want to count multiple occurances of each special word? My answer gives the count by line including multiple occurances. Also, it reminded me to add \Q and \E for safety's sake, so I've made that change. Update 2: For those who don't recognize it, see Perl Idioms Explained - my $count = () = /.../g. -xdg Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
In Section
Seekers of Perl Wisdom
|
|