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

Re^3: regex, find words that occur more than once.

by GrandFather (Saint)
on Sep 15, 2020 at 21:25 UTC ( #11121814=note: print w/replies, xml ) Need Help??

in reply to Re^2: regex, find words that occur more than once.
in thread regex, find words that occur more than once.

I completely agree with you on the use of white space.

The defined or initialization ($reps{$1} //= 1;) is completely unnecessary code bloat, partly because the values in the hash are only marginally important. For purposes of answering the question "Has this word repeated" $words{$1} = undef; works just as well and the following increment is not required.

The post increment ($words{$1}++;)I should have picked up in the OP. It is mindless cargo culting with no redeeming qualities. Unless logic requires otherwise always pre-increment (++$words{$1};. Pre-increment is never slower (and often faster) than post-increment - which is of trivial importance almost always. Much more important is that sticking the operator out the front makes it easier to be seen making the code easier to understand and maintain.

To my mind the most important aspect of the words hash is that it is a list of words - the keys are more important than the values. The name of the hash tells you that. Especially where a hash is being used to collect unique instances of things, realizing that keys can be the important data and the values are incidental is a liberating break through.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^4: regex, find words that occur more than once.
by kennethk (Abbot) on Sep 15, 2020 at 23:28 UTC

    Hopefully I properly qualified my comments since I tried to offer them as food for thought rather than some prescriptive set of requirements.

    The pre-increment is a good suggestion, and aligns with a verb-object syntax nicely. I need to break that muscle memory.

    The subject of the post agrees with your interpretation of the hash, but the usage runs counter. As soon as the goal becomes "How many times are words repeated?" the approach should have an accurate count in whatever stash you are using, as close to data as possible. Obviously the best answer is what you gave in Re: regex, find words that occur more than once., but subject to a homework-like requirement of use a regex a defined-or assignment keeps the store honest while still being reasonably succinct (and doesn't net add any lines). I'm not fundamentally opposed to communicating that the keys are the important thing, but especially when people are starting out, having the hash named after the property you are measuring (e.g., %count, %seen, %formatted) avoids %words, %others, %words2 anti-patterns.

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

Re^4: regex, find words that occur more than once.
by parv (Vicar) on Sep 16, 2020 at 02:54 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11121814]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2021-03-01 01:47 GMT
Find Nodes?
    Voting Booth?

    No recent polls found