Jun 22, 2008

count no. of occurances of a word

Example1 :
#########

#!F:\Perl\bin\perl -w
use strict;

my $text = 'perl is good, perl is better, perl is best';
my $count = ($text =~ s/perl/perl/g);
print "\n No. of occurances of 'perl' is:", $count;
print "\n";


Example2 :
#########

#!F:\Perl\bin\perl -w
use strict;

my $p = "india s great country india india super";
my $find = "india";
my $count = () = $p =~ /$find/g;
print "\n Count:", $count;
print "\n";


Example3:
########

use strict;
my($test,$number);
$test = "12344tyyyyy456";
$number = ($test =~ tr/[0-9]/[0-9]/);
print "Number of digits in variable is :- $number ";

No comments:

Post a Comment