Check the frequency : Frequency « Regular Expression « Perl






Check the frequency

   
#!/usr/bin/perl

use warnings;
use strict;

sub frequency {
    my $text = join('', @_);
    my %letters;
    foreach (split //, $text) {
        $letters{$_}++;
    }
    return %letters;
}

my $text = "this is a test";

my %count = frequency($text);

foreach (sort keys %count) {
    print "\t", $count{$_}, " '$_", ($count{$_} == 1)? "'": "'s", "\n";
}

   
    
    
  








Related examples in the same category

1.Match zero or one characters
2.To specify a maximum number of occurrences, use 0 as the lower bound.
3.To specify a minimum number of occurrences, leave off the upper bound.
4.Checking for multiple occurrences