avg word length / avg sentence length - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

avg word length / avg sentence length

ecbruschiecbruschi Posts: 4Member
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!!!

Comments

  • JonathanJonathan Posts: 2,914Member
    : 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:-

    [code]# 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;
    [/code]

    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.");

  • ecbruschiecbruschi Posts: 4Member
    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?
  • JonathanJonathan Posts: 2,914Member
    [b][red]This message was edited by Jonathan at 2003-11-21 11:13:48[/red][/b][hr]
    : 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?
    [code]open my $fh, "< input.txt"; # Replace the filename. :-)
    my $textIn = join('', <$fh>);
    close $fh;[/code]

    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.");



  • ecbruschiecbruschi Posts: 4Member
    it says missing $ at loop variable at average.pl on line 12...
  • JonathanJonathan Posts: 2,914Member
    : 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.");

  • ecbruschiecbruschi Posts: 4Member
    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;

  • JonathanJonathan Posts: 2,914Member
    : 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.");

  • JonathanJonathan Posts: 2,914Member
    As for the most likely fix...try this:-

    [code]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;
    [/code]

    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.");

Sign In or Register to comment.