How to count no of occurrence of a unique patterns in perl?

Answers were Sorted based on User's Feedback



How to count no of occurrence of a unique patterns in perl? ..

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

How to count no of occurrence of a unique patterns in perl? ..

Answer / ramya

while ($string =~ /<pattern>/g) { $count++ }

Is This Answer Correct ?    9 Yes 3 No

How to count no of occurrence of a unique patterns in perl? ..

Answer / shah faisal

($count)=$string =~ s/<pattern>/<pattern>/g;

Is This Answer Correct ?    4 Yes 3 No

How to count no of occurrence of a unique patterns in perl? ..

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

Post New Answer

More CGI Perl Interview Questions

Differentiate between arrays and list in perl.

0 Answers  


What are some common methods to all handles in perl?

0 Answers  


Explain strftime() function in perl?

0 Answers  


What does file test operators do in perl?

0 Answers  


Explain the difference between my and local?

2 Answers  






Define print() function in perl?

0 Answers  


Which operator in perl is used for the concatenation of two strings?

0 Answers  


What is the function of virtual documents in cgi programs?

0 Answers  


What is the difference between die and exit in perl?

0 Answers   HCL,


Write a program to show the process of spawning a child process

0 Answers  


Write a cgi program to show the header part?

0 Answers  


what are steps to do to lock the sony ericsson mobile with password?

0 Answers  


Categories