Word frequency: Difference between revisions
m
→{{header|Perl}}: future-proof for 5.36, 'use utf8'
m (→{{header|Raku}}: fix indent) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: future-proof for 5.36, 'use utf8') |
||
Line 3,325:
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">
use warnings;
use utf8;
my $top = 10;
open $fh, "<", '135-0.txt';▼
($text = join '', <$fh>) =~ tr/A-Z/a-z/▼
@matcher = (▼
▲(my $text = join '', <$fh>) =~ tr/A-Z/a-z/;
▲my @matcher = (
qr/[a-z]+/, # simple 7-bit ASCII
qr/\w+/, # word characters with underscore
Line 3,337 ⟶ 3,340:
);
for my $reg (@matcher) {
print "\nTop $top using regex: " . $reg
my @matches = $text =~ /$reg/g;
my %words;
for my $w (@matches) { $words{$w}++ };
my $c = 0;
for my $w ( sort { $words{$b} <=> $words{$a} } keys %words ) {
printf "%-7s %6d\n", $w, $words{$w};
last if ++$c >= $top;
Line 3,350 ⟶ 3,353:
{{out}}
<pre>
the 41089
of 19949
Line 3,384 ⟶ 3,388:
was 8621
that 7924
it 6661
</pre>
=={{header|Phix}}==
|