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 |
What are the advantages of programming in perl?
How can you use Perl warnings and what is the importance to use them?
Write a program to decode the data in the form using cgi programming
What happens when you return a reference to a private variable?
What does `new $cur->{LINK}' do? (Assume the current package has no new() function of its own.)
Explain arrays in perl.
What is the difference between $array[1] and @array[1]?
What does read () command do?
There are some duplicate entries in an array and you want to remove them. How would you do that?
How can arrays be tied?
Where the command line arguments are stored and if you want to read command-line arguments with Perl, how would you do that?
What is the usage of -i and 0s options?