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
“Perl regular expressions match the longest string possible”. What is the name of this match?
How many types of primary data structures in Perl and what do they mean?
package MYCALC; use Exporter; our @EXPORT = (); our @ISA = qw(Exporter); our @EXPORT_OK = qw(addition multi); our %EXPORT_TAGS = (DEFAULT => [qw(&addition)],Both => [qw(&addition & +multi)]); sub addition { return $_[0] + $_[1]; } sub multi { return $_[0] * $_[1]; } 1; Program: use strict; use warnings; my @list = qw (2 2); use Module qw(:DEFAULT); print addition(@list),"\n"; Above coding is my module MYCALC and the program which using this module, I have not exported any function using @EXPORT, but I have used the DEFAULT in %EXPORT_TAGS with the function addition, when I call this function from the main it says the error as,
What does file test operators do in perl?
How to disable the mod_perl from apache_server as i have used perlfect search on the site and its pagination is not working and the remedy is to disable the mod_perl.
What is 'commit' command in perl?
What is the closure in PERL?
Explain what is lvalue?
Explain goto label, goto name, and goto expr?
What does redo statement do in perl?
Distinguish my and local?
What are the advantages and disadvantages of perl language?
What does the’$_’ symbol mean?
Which functions in perl allows you to include a module file.
How do I print the entire contents of an array with Perl?