Tuesday, July 19, 2011

Finding the occurence of word in line perl

Perl Program:

#!/usr/bin/perl
$line = "The most hello great hello great important";
@array=split(/hello/,$line);
$count=scalar(@array);
$count = $count -1 ;
print "The no of occurences of hello in line is $count";

OUTPUT:

G:\perl_programs>perl perl_no_of_words.pl
The no of occurences of hello in line is 2
G:\perl_programs>


Analysis:
Here is split function is used to split line based on the word hello.And the no of split words will be 1 more than the occurence of the word.So we need to subtract 1.

No comments:

Post a Comment