Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear Perl monks,

I am trying to write a script that will allow me to count words for each individual line of a file (the file contains 64 sentences, so I would like 64 different word counts, next to the relevant sentence).

I am brand new to Perl and so far have managed to cobble together the following, which gives me total words in the file, lines in the file, characters in the file, average words per line, average characters per word, and even characters per line(including spaces), but now I have come to you as I cannot work out how I get a word count for each line.

#!/usr/local/bin/perl #MB Nov 2012 #script to check experiment stimuli #counts the number of lines in a file #counts number of words in a file and averages across lines #count the number of characters in a file and averages across words #BUT how do I count the number of words ON EACH LINE? while (<>) { chop; foreach $w (split) { $words++; $char = $char + length($w); #cumulative characters for all words } #$wordline = length($_); # this gives the character count per line ins +tead of the word count; } #print "\n$. the sentence [$_] has $words words"; # this prints the se +ntence(good!) but counts the cumulative number of words for all sente +nces until that point instead of that sentence only print "\n$words words in this file\n"; $avwds = $words/$.; #average words per line $avch = $char/$words; #average characters per word print "there are $char characters in total"; print "\nthere are $. lines with an average of $avwds words per line"; print "\nthere are $words words with an average of $avch characters pe +r word";

I feel as though I should be able to make a list of words in each line, and then I could count the length of that list for each line, but I am not sure how to do this and how to integrate it with the foreach word loop I already have.

Any advice or pointers appreciated,


In reply to separate word count for each line of a file by melb100

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [1nickt]: That's cool Disci+

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (4)
    As of 2018-01-24 03:02 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (255 votes). Check out past polls.