Word frequency: Difference between revisions

m
→‎{{header|Perl}}: future-proof for 5.36, 'use utf8'
m (→‎{{header|Raku}}: fix indent)
m (→‎{{header|Perl}}: future-proof for 5.36, 'use utf8')
Line 3,325:
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">$topuse = 10strict;
use warnings;
use utf8;
 
my $top = 10;
open $fh, "<", '135-0.txt';
($text = join '', <$fh>) =~ tr/A-Z/a-z/
or die "Can't open '135-0.txt': $!\n";
 
open my $fh, "'<"', '135ref/word-0count.txt';
@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 . "\n";
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>
<pre>Top 10 using regex: (?^:[a-z]+)
the 41089
of 19949
Line 3,384 ⟶ 3,388:
was 8621
that 7924
it 6661</pre>
</pre>
 
=={{header|Phix}}==
2,392

edits