http://www.perlmonks.org?node_id=1004982


in reply to separate word count for each line of a file

Hint: the following line is where you count the words in every line. The only difference is that you add the count to a total word count (every word in the document) as opposed to just that line. This sounds like homework so I hesitate to just give you the answer...but maybe the hint is enough to go off of.

foreach $w (split) { $words++; # Increment the word count by one for each word. $char = $char + length($w); #cumulative characters for all words }