A word-count program that handles multiple spaces and tabs between words. : Escape « Regular Expression « Perl






A word-count program that handles multiple spaces and tabs between words.

    

#!/usr/local/bin/perl 

$wordcount = 0; 
$line = <STDIN>; 
while ($line ne "") { 
   chop ($line); 
   @words = split(/[\t ]+/, $line); 
   $wordcount += @words; 
   $line = <STDIN>; 
} 
print ("Total number of words: $wordcount\n"); 

   
    
    
    
  








Related examples in the same category

1.Escape Sequences for Special Characters
2.Escape sequences, \n and \t
3.Match the new line character
4.Match multiline patterns?
5.Skip blank lines and comments
6.Multiplies every integer in a file by 2