Saturday, July 30, 2011

finding the count of a certain character in a line perl


Perl Program:

$_="great,awesome.wonderful  wonder";
$dd=tr/w//;
print "count of character w is :$dd";

Output:

G:\perl_programs>perl perl_tr.pl
count of character w is :3

G:\perl_programs>
Analysis:
Here i have used tr operator to find the count of character 'w'.tr operator is normally used to translate the characters.But in this case i have translatted 'w' into
nothing as per the pattern "tr/w//".So it just returns the no of character matched in the line.

No comments:

Post a Comment