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 will you declare a variable in perl?
We all know private variables exist in perl. But do private METHODS exist in perl ? Eg ?
Which operator in perl is used for the concatenation of two strings?
How do you set environment variables in perl?
Can you add two arrays together?
Why is it hard to call this function: sub y { "because" } ?
What are some common methods to all handles in perl?
List the data types that Perl can handle?
How would you ensure the re-use and maximum readability of your perl code?
Which functions in Perl allows you to include a module file or a module and what is the difference between them?
Explain which feature of PERL provides code reusability?
How can you call a subroutine and identify a subroutine?
Explain subroutine?
What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash?
Explain about the applications of perl?