Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: Count number of occurrences of a list of words in a file (updated)by AnomalousMonk (Archbishop) |
on May 09, 2018 at 16:32 UTC ( [id://1214288]=note: print w/replies, xml ) | Need Help?? |
You're enabling warnings and strictures, and that's very good, but in the OPed code you have two variables that are undeclared (that I can see): @listes and $file (update: now fixed). This code will not compile, and it's very important to present compilable code to the monks lest they grow grumpy. Please see Short, Self-Contained, Correct Example. That said, the word counting loop jumps out at me. My thought was similar to fleet-fingered Athanasius's here (update: and almost-as-fleet-fingered Tux's here :), but rather than using split to exclude what is not wanted, I'd suggest defining a pattern $rx_word to extract anything that looks like a word that you might want to count. If the extracted word is in the pre-existing %count hash, count it. Something like (untested): Obviously, the proper definition of $rx_word is critical! Only you can determine what this proper definition is. (If you define it right, you don't even need to bother chomp-ing anything.)
(I would have suggested the technique described in haukex's Building Regex Alternations Dynamically, Update: I had thought that there was a hard limit to regex alternations that would cause compilation to fail, but it seems there is not — or if there is, it's much greater than 60K words! What I may have been thinking of is a limit to trie-optimization that causes a fallback to literal ordered alternation at some point. Given that that's the case, I would encourage you to try the dynamic alternation technique. I now expect it to work, and the only question is if it has a speed advantage. Give a man a fish: <%-{-{-{-<
In Section
Seekers of Perl Wisdom
|
|