CoderZone.org

Category: >> Perl Code >> Count words or terms Bookmark and Share

<< lastnext >>

Snippet Name: Count words or terms

Description: Count the number of times a given word or character appears in a string.

Comment: (none)

Author: CoderZone
Language: PERL
Highlight Mode: PERL
Last Modified: March 05th, 2009

#!/usr/bin/perl
 
# count negative numbers
while ($string =~ /-\d+/g) { $count++ }
print "There are $count negative numbers in the string";
 
# count asterisks:
$count = ($sentence =~ tr/*/*/);
 
# count the number of lines containing the word "Sally";
$totalSallys++ if ($line =~ /sally/i)
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 5