Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'd reverse the nature of the hash and store lines as keys and values as the count.

use strict; use warnings; my @words = qw( alpha beta gamma ); my @lines = ( 'alpha comes before beta and gamma, but not alphabet', 'beta comes after alpha; beta comes before gamma', 'gamma is the last word', ); my %count; for my $line ( @lines ) { for my $word ( @words ) { my $c =()= ($line =~ m/\b\Q$word\E\b/g); $count{$line} += $c; } } print map { "$count{$_}: $_\n" } @lines;

Gives:

3: alpha comes before beta and gamma, but not alphabet 4: beta comes after alpha; beta comes before gamma 1: gamma is the last word

(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 reply to Re: Increasing key/values held in a Hash from an Array by xdg
in thread Increasing key/values held in a Hash from an Array by Gavin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found