Using tr to count the occurrences of specific characters. : tr « Regular Expression « Perl






Using tr to count the occurrences of specific characters.

     
#!/usr/local/bin/perl 

$punctuation = $blanks = $total = 0; 
while ($input = <STDIN>) { 
    chop ($input); 
    $total += length($input); 
    $_ = $input; 
    $punctuation += tr/,:;.-/,:;.-/; 
    $blanks += tr/ / /; 
} 
print ("In this file, there are:\n"); 
print ("\t$punctuation punctuation characters,\n"); 
print ("\t$blanks blank characters,\n"); 
print ("\t", $total - $punctuation - $blanks); 
print (" other characters.\n"); 

   
    
    
    
    
  








Related examples in the same category

1.Counting using tr.
2.$text =~ tr/a-z/d-za-c/;
3.$text =~ tr/d-za-c/a-z/;
4.Using tr to retrieve the length of a string.
5.tr/a-z/A-Z/
6.tr/i/o/;
7.tr/o/i/;