Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

How to concatenate strings in perl?

1032


Explain the use of 'my' keyword in perl?

1009


How do I generate a list of all .html files in a directory?

944


What is the difference between perl list and perl array?

858


Write a program to concatenate the $firststring and $secondstring and result of these strings should be separated by a single space.

1044


Why is it hard to call this function: sub y { "because" } ?

947


explain the various functions/directives in perl that allow you to include/import a module. Also, state the differences between them.

860


Who created perl?

911


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

939


Show the use of sockets for the server and client side of a conversation?

882


What is the difference between use and require in perl?

856


Give an example of using the -n and -p option.

991


Write a program to download the contents from www.perlinterview.com/answers.php website in Perl.

882


How can you replace the characters from a string and save the number of replacements?

891


Mention what is cpan?

1010