The code I gave you was written on the fly on the command line rather than being stored in a script file. Therefore it would require a little modification to be used as a stored script. The enclosing single quotes around the code and the -E flag would go and the command line -M flags would be incorporated in the script as
use strict; use warnings;
lines at the top of your code. I use q{...} and qq{...} instead of '...' and "..." because it makes it easier to write code on the command line in both Unix/Linux and MS Windows environments but they are fuctionally equivalent.
Some points about your translation:-
Be consistent with indenting, indent code that falls within inner logical scopes so that all code at the same logical level starts in the same column. Indent continuation lines as well but a different amount from logical indentation to avoid confusion.
Use the three-argument form of open and lexical rather than package filehandles and when you die of failure show the o/s error message held in $! as well, see perlvar.
My normal practice is to use camelCase for my identifiers and, when applied to files that I am opening, I would use, say, $wordsFile to hold the name of the file I want to open and $wordsFH for the lexical filehandle I open it against and <$wordsFH> to read it. You might have a typo, is your file called "woords.txt" or "words.txt" and where did you get $filename from?
Lose the second closing brace in the last line, it is not an error but you forgot to remove it when you converted to double-quotes and it will make your output look untidy.
You could employ a do block to get the total number of hits by changing
$words{$1} ++ while $text =~ m{$rxWords}g;
to
my $totalHits; do { $totalHits ++; $words{$1} ++; } while $text =~ m{$rxWords}g;
I'll leave you to see if you can work out how to get the total number of words given these clues; the regex pattern \b\w+\b and the g match modifier. Play around with some simple test text and see if you can solve the problem for yourself then apply it to your real code. Doing is far and away the best way of learning!
I hope this helps you move forward.
Cheers,
JohnGG
In reply to Re^3: count number of overlapping words in a document
by johngg
in thread count number of overlapping words in a document
by dmarcel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |