Hello, I am new begginer to perl programming and I need help with a program. I have a text file that is based on the Gettysburg Address. I need to do the following:

read the file, print out the file to the screen, sort the words in alphabetical order, print out the sorted file, calculate how many times each word occurs in the address and print out the results of this calculation and print out the word that most often occurs in the address and how often it occurs.

3 Years
Discussion Span
Last Post by 2teez

Hello perlbegginer02,

This sound to me like an assignment. However, if you ask for help atleast you are suppose to show some effort on your part. The Gettysburg Address you didn't post and there was no script whatsoever.
I will give show how to solve this but you will have to open your file yourself using perl open function.

To this problem you must think of removing all non words and putting all words to lower case so that one can properly account for all words.

See this:

use warnings;
use strict;
use List::Util qw(max);

my %speech_analyse;
my $splitter = qr/\W+|\s+/;

$speech_analyse{ lc $_ }++ for split /$splitter/, do {
    local $/;

my $str_max_len = max map length($_) => keys %speech_analyse;

my $highest_occurrence = max values %speech_analyse;

print "Word\tNumber of times of Occurence", $/;

for ( sort { $a cmp $b } keys %speech_analyse ) {
    my $space = qq{ } x ( $str_max_len - length($_) );
    print sprintf "%s  %s  %s\n" => $_,
      $space, $speech_analyse{$_}, $/;

print sprintf "Highest Occur Word(s) is %s Ocurred for %d times\n",
  uc $_, $highest_occurrence
  for grep { $speech_analyse{$_} eq $highest_occurrence } keys %speech_analyse;

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.

The solution to this yield the following:

Word    Number of times of Occurence
a              7
above          1
add            1
advanced       1
ago            1
all            1
altogether     1
and            6
any            1
are            3
as             1
battle         1
be             2
before         1
birth          1
brave          1
brought        1
but            2
by             1
can            5
cause          1
civil          1
come           1
conceived      2
consecrate     1
consecrated    1
continent      1
created        1
dead           3
dedicate       2
dedicated      4
detract        1
devotion       2
did            1
died           1
do             1
earth          1
endure         1
engaged        1
equal          1
far            2
fathers        1
field          2
final          1
fitting        1
for            5
forget         1
forth          1
fought         1
four           1
freedom        1
from           2
full           1
gave           2
god            1
government     1
great          3
ground         1
hallow         1
have           5
here           8
highly         1
honored        1
in             4
increased      1
is             3
it             5
larger         1
last           1
liberty        1
little         1
live           1
lives          1
living         2
long           2
measure        1
men            2
met            1
might          1
nation         5
never          1
new            2
nobly          1
nor            1
not            5
note           1
now            1
of             5
on             2
or             2
our            2
people         3
perish         1
place          1
poor           1
portion        1
power          1
proper         1
proposition    1
rather         2
remaining      1
remember       1
resolve        1
resting        1
say            1
score          1
sense          1
seven          1
shall          3
should         1
so             3
struggled      1
take           1
task           1
testing        1
that           13
the            11
their          1
these          2
they           3
this           4
those          1
thus           1
to             8
under          1
unfinished     1
us             3
vain           1
war            2
we             10
what           2
whether        1
which          2
who            3
will           1
work           1
world          1
years          1
Highest Occur Word(s) is THAT Ocurred for 13 times
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.