Example 5-3. examples/hash/count_words.p6
#!/usr/bin/perl6
die "Usage: $PROGRAM_NAME FILENAME\n" if not @*ARGS;
my $filename = @*ARGS[0];
my %counter;
my $fh = open $filename, :r err die "Could not open '$filename'";
for =$fh -> $line {
my @words = split /\ +/, $line;
for @words -> $word {
%counter{$word}++;
}
}
for %counter.keys -> $word {
say "$word %counter{$word}";
}| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| Multidimensional hashes | Up | Overview |