Hey guys, I started to look at Perl about 3 days ago and I've managed to created a program that counts the word frequency in a given text file. Like so:
#!/usr/bin/local/perl use strict; use warnings; my %count; my $file_name = shift or die "Usage: perl $0 [FILE]\n"; open my $fh, '<', $file_name or die "Could not open '$file_name' $!"; while (my $line = <$fh>) { chomp $line; foreach my $word (split /\s+/, $line) { $count{$word}++; } } foreach my $word (sort keys %count) { printf "%-31s %s\n", $word, $count{$word}; }
The only thing is, is that this program shows the frequency of every word in the appeared order of the text file. What would I need to add, to be able to not only get the before mentioned result but also listing the ten most commonly used words in descending order?
Worth noting is this is my first post on here so please inform me if I'm doing anything wrong.
I'm thankful for any tips or advice
Thanks in advance.
In reply to How do I create a list with the 10 most frequently used words in a file? by Jannejannesson
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |