How to count no of occurrence of a unique patterns in perl?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / shah faisal
($count)=$string =~ s/<pattern>/<pattern>/g;
| Is This Answer Correct ? | 4 Yes | 3 No |
Answer / perllearner
my %hshUniq;
my $testString = "This is the programme to check the count of unique values present in this text This is the programme to check";
my @arrList = split (" ",$testString);
foreach (@arrList);
{
chomp($_);
if ( exist %hshUniq{$_})
{
my $count = $hshUniq{$_};
$hshUniq{$_} = $count++;
}else
{
$hshUniq{$_} = 1;
}
}
while (($key,$value) = each(%hshUniq))
{
print "The unique character $key count in the string is $value
";
}
| Is This Answer Correct ? | 0 Yes | 0 No |
If you want to empty an array then how would you do that?
How can I display all array element in which each element will display on next line in perl ?
How to change a directory in perl?
What is the use of -t?
How to merge two arrays in perl?
How would you trap error occurred in the perl program/file?
What are the two ways to get private values inside a subroutine?
How would you replace a char in string and how do you store the number of replacements?
How to open and read data files with Perl
What is the use of -n and -p options?
how to create a flat file database as shown below s.no name age city phone 0 hema 22 Calcutta 4312542 1 hema 21 Bangalore 2344345 2 ganesh 25 delhi 2445454 3 kartik 45 pune 4312121 4 santosh 25 Hyderabad 2254231 5 kumar 25 mysore 2344567 6 gita 34 mangalore 6532123 7 gita 32 pune 2213456 Q1.print the details of the person who r from bangalore q2.Replace the city name managlore to pune q3.prints no of person having name gita and hema q4.print how many are of age 25.
What is the difference between having a parenthesis after module name and without parenthsis after module name?? i.e Package::Module(); and Package::Module;