To find the most frequently-used words in a text, do something like this:
#!/usr/bin/perl -w
use strict;
use warnings 'all';
# Read the text:
open my $ifh, '<', 'text.txt'
or die "Cannot open file: $!";
local $/;
my $text = <$ifh>;
# Find all the words, and count how many times they appear:
my %words = ( );
map { $words{$_}++ }
grep { length > 1 && $_ =~ m/^[\@a-z-']+$/i }
map { s/[",\.]//g; $_ }
split /\s/, $text;
print "Words, sorted by frequency:\n";
my (@data_line);
format FMT =
@<<<<<<<<<<<<<<<<<<<<<<... @########
@data_line
.
local $~ = 'FMT';
# Sort them by frequency:
map { @data_line = ($_, $words{$_}); write(); }
sort { $words{$b} <=> $words{$a} }
grep { $words{$_} > 2 }
keys(%words);
Example output looks like this:
john@ubuntu-pc1:~/Desktop$ perl frequency.pl
Words, sorted by frequency:
for 32
Jan 27
am 26
of 21
your 21
to 18
in 17
the 17
Get 13
you 13
OTRS 11
today 11
PSM 10
Card 10
me 9
on 9
and 9
Offline 9
with 9
Invited 9
Black 8
get 8
Web 7
Starred 7
All 7
View 7
Obama 7