Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    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-02-25 02:23 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found