Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
avg word length / avg sentence length Posted by ecbruschi on 21 Nov 2003 at 10:25 AM
How do i Write a perl script to computer the average word length ( in characters) and the average sentence length (in words) of a text file??? Need to know ASAP!!!
Report
Re: avg word length / avg sentence length Posted by Jonathan on 21 Nov 2003 at 10:49 AM
: How do i Write a perl script to computer the average word length (
: in characters) and the average sentence length (in words) of a text
: file??? Need to know ASAP!!!
I'll leave you to read the text file into the variable $textIn, that's the easy bit.

As for doing the calculations:-

# Some variables to store the results and workings in.
my $avWordLength = 0;
my $avSentenceLength = 0;
my @wordLengths = ();
my @sentenceLengths = ();

# Get lengths.
push @wordLengths, length($_) for split(/\W+/, $textIn);
push @sentenceLengths, length($_) for split(/\.\s+/, $textIn);

# Do averages.
$avWordLength += $_ for (@wordLengths);
$avWordLength /= scalar @wordLengths;
$avSentenceLength += $_ for (@sentenceLengths);
$avSentenceLength /= scalar @sentenceLengths;


Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: avg word length / avg sentence length Posted by ecbruschi on 21 Nov 2003 at 10:55 AM
About reading the text file into the variable $textIn,
I don't know how to do that either :-/...

Thank u for the rest tho...couldu still show me to how do that?
Report
Re: avg word length / avg sentence length Posted by Jonathan on 21 Nov 2003 at 11:13 AM
This message was edited by Jonathan at 2003-11-21 11:13:48

: About reading the text file into the variable $textIn,
: I don't know how to do that either :-/...
:
: Thank u for the rest tho...couldu still show me to how do that?
open my $fh, "< input.txt"; # Replace the filename. 
my $textIn = join('', <$fh>);
close $fh;


Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");



Report
Re: avg word length / avg sentence length Posted by ecbruschi on 21 Nov 2003 at 11:16 AM
it says missing $ at loop variable at average.pl on line 12...
Report
Re: avg word length / avg sentence length Posted by Jonathan on 21 Nov 2003 at 11:21 AM
: it says missing $ at loop variable at average.pl on line 12...
Uh...and line 12 is (in the way you've laid it out)? I did test that code before posting it...

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: avg word length / avg sentence length Posted by ecbruschi on 21 Nov 2003 at 11:23 AM
This is the code i have:
(my file is called input.txt that has also the information in it)

open my $fh, "< input.txt";
my $textIn = join('', <$fh>);
close $textIn;

my $avWordLength = 0;
my $avSentenceLength = 0;
my @wordLengths = ();
my @sentenceLengths = ();


push @wordLengths, length($_) for split(/\W+/, $textIn);
push @sentenceLengths, length($_) for split(/\.\s+/, $textIn);


$avWordLength += $_ for (@wordLengths);
$avWordLength /= scalar @wordLengths;
$avSentenceLength += $_ for (@sentenceLengths);
$avSentenceLength /= scalar @sentenceLengths;

Report
Re: avg word length / avg sentence length Posted by Jonathan on 21 Nov 2003 at 11:30 AM
: This is the code i have:
: (my file is called input.txt that has also the information in it)
What version of Perl are you using? I tested this under 5.8, and just tested it under Perl 5.6.1, and it worked fine on both of those...

perl -v

At the command line will reveal all.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: avg word length / avg sentence length Posted by Jonathan on 21 Nov 2003 at 2:12 PM
As for the most likely fix...try this:-

open my $fh, "< input.txt";
my $textIn = join('', <$fh>);
close $textIn;

my $avWordLength = 0;
my $avSentenceLength = 0;
my @wordLengths = ();
my @sentenceLengths = ();
my $temp = '';

push @wordLengths, length($temp) foreach $temp (split(/\W+/, $textIn));
push @sentenceLengths, length($_) foreach $temp (split(/\.\s+/, $textIn));

$avWordLength += $temp foreach $temp (@wordLengths);
$avWordLength /= scalar @wordLengths;
$avSentenceLength += $temp foreach $temp (@sentenceLengths);
$avSentenceLength /= scalar @sentenceLengths;


Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.