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
Where the command line arguments are stored and if you want to read command-line arguments with Perl, how would you do that?
What is a chop() function in perl?
What does perl do in linux?
Can inheritance be used in perl? Explain with the help of an example.
What does cgi program store?
What can be done for efficient parameter passing in perl? Explain.
There are two types of eval statements i.e. Eval expr and eval block. Explain them.
Comment on data types and variables in perl.
What does redo statement do in perl?
What are the arguements we normally use for perl interpreter?
How to turn on Perl warnings? Why is that important?
Explain the different types of data perl can handle.
Explain join function in perl?
How to remove a directory in perl?
Which of these is a difference between Perl and C++ ?