hi
i got a file called essay which contain few pages with many paragraphs. now i wanna with PERL to produce another file which called Essaylist that contain a sorted list of words that appear in the file essay.
the format for Essaylist:
$word found $times times on page a b c....
where $word is the word appear in the file, $times is the times that the word appear in the file and a b c is the page number that the word appeared.
i did something like that
file = essay
# Nothing has been seen
%seen = ();
# read words from input
while (<$file>)
{
while ( /(\w['\w-]*)/g)
{
# Increment the counter
$seen{lc $1}++;
}
}
# output hash in a
foreach $word (sort { } keys %seen)
{
print "$word found $seen{$word} times on pages $page\n";
}
the code above is not working, so can anyone please help me?
file = essay ## it should be $file
# Nothing has been seen
%seen = ();
# read words from input
# where did you open the file ??
while (<$file>)
{
### Missing chomp
while ( /(\w['\w-]*)/g)
{
# Increment the counter
$seen{lc $1}++;
}
}
# output hash in a
foreach $word (sort { } keys %seen)
{
print "$word found $seen{$word} times on pages $page\n";
}
here is my try
#! /usr/bin/perl
my $file = $ARGV[0] ;
my @LineArray = () ;
my %WordHash = () ;
open(ESSAY,"$file") or die "can not open file for reading : $!\n" ;
while(<ESSAY>)
{
chomp ;
@LineArray = split (/ \s*/,$_ ) ;
foreach my $word ( @LineArray )
{
$word = lc $word ;
$WordHash{$word}++ ;
}
}
foreach my $word ( sort keys %WordHash )
{
print "$word\t$WordHash{$word}\n" ;
}