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
How to concatenate strings in perl?
Explain the use of 'my' keyword in perl?
How do I generate a list of all .html files in a directory?
What is the difference between perl list and perl array?
Write a program to concatenate the $firststring and $secondstring and result of these strings should be separated by a single space.
Why is it hard to call this function: sub y { "because" } ?
explain the various functions/directives in perl that allow you to include/import a module. Also, state the differences between them.
Who created perl?
How do I print the entire contents of an array with Perl?
Show the use of sockets for the server and client side of a conversation?
What is the difference between use and require in perl?
Give an example of using the -n and -p option.
Write a program to download the contents from www.perlinterview.com/answers.php website in Perl.
How can you replace the characters from a string and save the number of replacements?
Mention what is cpan?