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 -> symbol in perl?
Explain ivalue?
Where do we require ‘chomp’ and what does it mean?
Explain cpan?
what is the main function of fork() in cgi programming?
“The methods defined in the parent class will always override the methods defined in the base class”. What does this statement means?
What is hash?
how to create a flat file database as shown below s.no name age city phone 0 hema 22 Calcutta 4312542 1 hema 21 Bangalore 2344345 2 ganesh 25 delhi 2445454 3 kartik 45 pune 4312121 4 santosh 25 Hyderabad 2254231 5 kumar 25 mysore 2344567 6 gita 34 mangalore 6532123 7 gita 32 pune 2213456 Q1.print the details of the person who r from bangalore q2.Replace the city name managlore to pune q3.prints no of person having name gita and hema q4.print how many are of age 25.
How to concatenate strings in perl?
What are the options that can be used to avoid logic errors in perl?
Give an example of the -i and 0s option usage.
Mention how many ways you can express string in Perl?
What are the different ways to run cgi?
How to merge two arrays in perl?
What is perl programming?