Click to See Complete Forum and Search --> : Perl


Krammer
November 18th, 2003, 08:44 PM
How do I find the longest word in the text file and print it in PERL...and compute the average word length and the average sentece length of a text file.

This is what I have so far....

print "What File would you like to open?";
$file= <STDIN>;

open INFILE, $file or die "File not Found \n";

@text = <INFILE>; #creates an array name @text, loads the contents of the file
print"\n";
print"\n"; print "array output begins here"; print "\n";
print @text;

print"\n"; print "\n"; print "** One Line at a Time \n";

$file = length;
$max1 = $file if $file > $max1;
print "$max1\n" if eof;



for($idx=0; $idx<=@text; $idx++)

{

{print substr(@text[$idx],0,7); print "\n";}

++$file;
}

khp
November 18th, 2003, 11:51 PM
Finding the longest word can be done using something like this

use Text::ParseWords;

print "What File would you like to open?";
$file= <STDIN>;

open INFILE, $file or die "File not Found \n";

@text = <INFILE>; #creates an array name @text, loads the contents of the file

@words = quotewords(" ",false,@text); # tokenize file
chomp(@words); # remove all trailing newline characters

for($idx=0; $idx<=@words; $idx++) # loop through all words
{
if (length($longest)<length(@words[$idx])) { # find the longest word so far
$longest=@words[$idx];
}
}
print "The longest word is\n";
print $longest . "\n";

This should also give you some idear, on how to solve the other problems.
But feel free to ask again if you continue to have problems.