How to count no of occurrence of a unique patterns in perl?
Answer Posted / kuldip singh behal
if we are looking for occurrences of single character in a
string then we can use TR command
syntax: tr///
example:
$string="The lion group will meet me after lunch";
and we are looking for the occurrences of character 'l' in
the given string. then we can write tr like
$count = ($string=~tr/l//);
print "l is coming $count time(s)";
but for multiple characters look up we have to use:
$string = "-9 55 48 -2 23 -76 4 14 -44";
while ($string =~ /-\d+/g) { $count++ }
print "There are $count negative numbers in the string";
Donated by :- Kuldip singh behal
| Is This Answer Correct ? | 10 Yes | 0 No |
Post New Answer View All Answers
What is 'commit' command in perl?
What is boolean context?
How do you you check the return code of a command in perl?
You want to print the contents of an entire array. How would you do that?
Explain returning values from subroutines?
Explain goto expr?
Explain regular expression in perl?
What are perl strings?
What is perl shift array function?
How and what are closures implemented in perl?
what are steps to do to lock the sony ericsson mobile with password?
How can I implement the function overloading in Perl ? I read about the operator overloading, I do not know how to implement the function overloading. Thanks in advance ?
how to get back up from private character editor which is saved in the format of .udf
How to copy a file in perl?
What are arrays in perl?