programming-examples/perl/Regular_Expression/Frequency.pl
2019-11-15 12:59:38 +01:00

67 lines
1.1 KiB
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";
}
#Match zero or one characters!
$p = "This is a pattern test.";
if ($p =~ /(\w)?/){ print "$1\n"; }
#To specify a maximum number of occurrences, use 0 as the lower bound.!
/de{0,3}f/ matches d, followed by no more than three es, followed by f.
#To specify a minimum number of occurrences, leave off the upper bound.!
/de{3,}f/ matches d, followed by at least three es, followed by f.
#Checking for multiple occurrences!
Pattern Interpretation
/a{1,4}/ Matches one, two, three, or four as.
/a{2}/ Matches two as.
/a{0,2}/ Matches one or two as.