Use Perl’s regex to count the # of words in a file

In the below code I use Perl’s regex capabilites to count the # of words in a file. Notice I used “\s” in the regex expression to account for not only spaces but also tab and new line characters. In addition, it is important to account for whitespace characters at the start of a sentence, because the “split” function would end up counting the starting whitespace as a valid entry.

Perl Code - File Word Count

This entry was posted in Perl, Programming Languages and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 × = sixty four