Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Good synthesis, as GrandFather says. A couple of minor stylistic elements you might consider:
  • You should put spaces around your Binding Operators. Regular expressions already look confusing to a lot of people. Same for before the parentheses of your for loop, but that's less of an issue.
  • One might naively expect that the hash in question contains the counts, and you correct that right before output. That could be confusing if the two loops were more complex or separated by a lot of code. Rather than fixing it at the end, you could initialize the value to 1:
    while($text=~/\b(\w+)\b(?=.*\b\1\b)/g){ $words{$1} //= 1; # initialize empty value to one $words{$1}++; }
    See Logical Defined Or and Assignment Operators for details on what I did there.
  • I usually name my hashes based upon how the key is related to the value. In this case, for example, %hits could be natural because when you look at it on the screen, you see the number of hits you got -- $hits{$word} reads to me as hits for word in English. On the other hand, %words sounds like I'm going to get words back, not a number. With that first change, it might be %count, %repetitions or %reps. Perl more than most programming languages is supposed to read following common grammar.
Full version with suggested modifications:
use strict; use warnings; use 5.10.0; my %reps; my $text = "and him him lad has him done and john has has"; while($text =~ /\b(\w+)\b(?=.*\b\1\b)/g){ $reps{$1} //= 1; $reps{$1}++; } foreach my $key (keys %words){ say "$key: $reps{$key}"; }

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re^2: regex, find words that occur more than once. by kennethk
in thread regex, find words that occur more than once. by Anonymous Monk

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 studying the Monastery: (7)
As of 2024-04-23 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found