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


Please Help Members By Posting Answers For Below Questions

“Perl regular expressions match the longest string possible”. What is the name of this match?

718


How many types of primary data structures in Perl and what do they mean?

1062


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,

2468


What does file test operators do in perl?

673


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.

1970


What is 'commit' command in perl?

812


What is the closure in PERL?

750


Explain what is lvalue?

753


Explain goto label, goto name, and goto expr?

711


What does redo statement do in perl?

764


Distinguish my and local?

708


What are the advantages and disadvantages of perl language?

679


What does the’$_’ symbol mean?

753


Which functions in perl allows you to include a module file.

658


How do I print the entire contents of an array with Perl?

731