count lines,words,punctuations and characters in perl script

Discussion in 'Perl Misc' started by angel, Nov 11, 2006.

  1. angel

    angel Guest

    hello im a perl beginner. is there anybody who can help me by giving me
    simple perl script? i need to know on how to count lines, words,
    punctuations and characters of a pharagraph. pls i really need help
    thanks a lot. i would really appreciate your help thanks!
     
    angel, Nov 11, 2006
    #1
    1. Advertising

  2. angel

    angel Guest

    hello thanks for the reply...where can i find that posting guidelines?

    i just need someone to help me with my script. im not really a
    programmer. perl is just one part of my course and its really my first
    time to join a group i just find it very interesting and educational.

    i already done half of my script but its the counting of the
    lines,words etc that i dont have any idea.

    here's my script:

    #!c:\perl\bin\perl.exe -w

    open(READFILE, "<StringText.txt"> || die "couldn't open file$!";

    while (<READFILE>)
    {
    print "$_";
    }
    close(READFILE);
    print"\n\nOutput:\n";

    open(READFILE, "<StringText.txt"> || die "couldn't open file:$!";
    while(<READFILE>)
    {
    while($_=~s/(\w+)(.*)/$2/)
    {
    $word= $2;
    $wordHash{$word}++;
    }
    while(($word, $count) = each(%wordHash))
    {
    $wordArray[$i] = "$word\t$count";
    $i++;
    chomp($count);
    print("words: $count\n");

    close(READFILE);

    print"\n\nString:\n";

    open(READFILE, "<StringText.txt"> || die "couldn't open file:$!";

    while(<READFILE>)
    {
    $_=~s/\s+//gi;
    $_=~s/\,//gi;
    $_=~s/\.//gi;
    $_=~tr/A_Z/a-z/;
    print"$_";
    }
    close(READFILE);

    the output should be:(from READFILE)

    Hey, diddle, diddle,
    The cat and the fiddle,
    The cow jumped over the moon.
    The little dog laughed
    To see such sport,
    And the dish ran away with the spoon.

    Output:
    lines: 6
    words:30
    characters: 154

    String:
    heydiddlediddlethecatandthefiddlethecowjumpedoverthemoonthelittledoglaughedtoseesuchsportandthedishwanawaywiththespoon

    i was able to do the first and the last (string) but its the counting
    that im having difficulty.





    wrote:
    > "angel" <> writes:
    > > hello im a perl beginner.

    >
    > Hello. Welcome.
    >
    > > is there anybody who can help me by giving me
    > > simple perl script?

    >
    > If you want to hire a programmer jobs.perl.org is down the hall. If
    > you want help to write your own script, go read the posting
    > guidelines, and then come back.
    >
    > > i need to know on how to count lines, words,
    > > punctuations and characters of a pharagraph.

    >
    > This smells like a homework assignment. Do you really think your
    > lecturer doesn't read this group?
    >
    > > pls i really need help
    > > thanks a lot. i would really appreciate your help thanks!

    >
    > What work have you done thus far, and how has it failed to meet your
    > needs?
    >
    > There was only one e. e. cummings -- you're not him. Use mixed case and
    > try to spell. Writing (good) programs requires clarity and precision
    > in any language. Practice being clear and precise in human languages,
    > too.
    >
    > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    > Lawrence Statton - s/aba/c/g
    > Computer software consists of only two components: ones and
    > zeros, in roughly equal proportions. All that is required is to
    > sort them into the correct order.
     
    angel, Nov 11, 2006
    #2
    1. Advertising

  3. angel

    angel Guest

    thanks for the reply...im a pel beginner and i will be using perl when
    i started making websites.

    thanks a lot!

    Michele Dondi wrote:
    > On 11 Nov 2006 06:50:44 -0800, "angel" <> wrote:
    >
    > >hello im a perl beginner. is there anybody who can help me by giving me
    > >simple perl script? i need to know on how to count lines, words,

    >
    > Yes, and chances are that some will do.
    >
    > >punctuations and characters of a pharagraph. pls i really need help
    > >thanks a lot. i would really appreciate your help thanks!

    >
    > However... are you a perl beginner or do you need to use perl for some
    > reason for once only? If the latter, than specify why: if it's a good
    > reason then it may be easier for some to pretend this is a help desk.
    > But generally we're not, and in the former case we prefer to ask you
    > to show us what you've tried thus far and what you're having
    > difficulties with.
    >
    > General purpose pointers:
    >
    > perldoc -q 'by paragraph'
    > perldoc -f split
    > perldoc -f m
    > perldoc -f length
    >
    >
    > Michele
    > --
    > {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    > (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    > .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    > 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    angel, Nov 11, 2006
    #3
  4. angel <> wrote:

    > where can i find that posting guidelines?



    They are posted to this newsgroup twice each week. Or see:

    http://www.augustmail.com/~tadmc/clpmisc.shtml


    > here's my script:
    >
    > #!c:\perl\bin\perl.exe -w
    >
    > open(READFILE, "<StringText.txt"> || die "couldn't open file$!";

    ^
    ^

    That is not a Perl program.

    Why aren't you asking about the syntax error that that lines generates?

    Is this your real code?


    > print "$_";


    perldoc -q vars

    What’s wrong with always quoting "$vars"?


    > while($_=~s/(\w+)(.*)/$2/)



    A loop that must iterate zero or one times is the same as an "if",
    so then, it should be written as an "if".

    If you expect it to iterate more than one time, then you need
    a "g" modifier on your s/// operator.


    > while(($word, $count) = each(%wordHash))
    > {
    > $wordArray[$i] = "$word\t$count";
    > $i++;



    If you use the push() function instead of indexing yourself, then you
    won't need to maintain $i.

    push @wordArray, "$word\t$count";



    [ snip TOFU ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 11, 2006
    #4
  5. angel wrote:
    > hello im a perl beginner. is there anybody who can help me by giving me
    > simple perl script? i need to know on how to count lines, words,
    > punctuations and characters of a pharagraph. pls i really need help
    > thanks a lot. i would really appreciate your help thanks!


    Two seconds of google yielded the following:

    http://www.stonehenge.com/merlyn/UnixReview/col02.html
     
    grocery_stocker, Nov 11, 2006
    #5
  6. angel

    angel Guest

    Hey, didle,didle,
    > The cat cat and the fiddle,
    > The cow jumped over the moon.
    > The little dog laughed
    > To see such sport,
    > And the dish ran away with the spoon.


    Output:
    lines: 6
    words: 30
    characters: 154

    i did some program but the output is different.

    i tried this:

    open(READFILE, "Stringtext.txt") || die "couldn't open files:$!";

    @_ = <READFILE>;
    $_ = join("",@_);
    $l_count = tr/\n//;
    $w_count = tr/A-Za-z//;
    $c_count = tr///cd;
    chomp($l_count);
    print("lines:" .$l_count,"\n");
    chomp($w_count);
    print("words:" .$w_count,"\n");
    chomp($l_count);
    print("characters:" .$c_count,"\n");

    close(READFILE);

    the output that i got when i run that program is:

    Output
    lines: 7 #should be 6
    words:116 # should be 30
    characters:152 # should be 154

    i dont know how it turned up that way...i need help..thanks




    grocery_stocker wrote:
    > angel wrote:
    > > hello im a perl beginner. is there anybody who can help me by giving me
    > > simple perl script? i need to know on how to count lines, words,
    > > punctuations and characters of a pharagraph. pls i really need help
    > > thanks a lot. i would really appreciate your help thanks!

    >
    > Two seconds of google yielded the following:
    >
    > http://www.stonehenge.com/merlyn/UnixReview/col02.html
     
    angel, Nov 13, 2006
    #6
  7. angel <> wrote:

    > @_ = <READFILE>;
    > $_ = join("",@_);



    You can replace that code with:

    $_ = do { local $/; <READFILE> };


    > $w_count = tr/A-Za-z//;



    tr/// works on _characters_, not on "words".

    You need a different operator.


    > chomp($w_count);



    Why do you expect a newline at the end of $w_count's value?

    If you don't expect a newline, then why are you trying to remove one?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 13, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chad
    Replies:
    2
    Views:
    1,408
    Peter Otten
    Mar 5, 2004
  2. Umesh
    Replies:
    17
    Views:
    788
    James Kanze
    Apr 26, 2007
  3. Umesh
    Replies:
    25
    Views:
    1,589
    James Kanze
    Apr 26, 2007
  4. revinna
    Replies:
    0
    Views:
    542
    revinna
    Aug 2, 2007
  5. basi
    Replies:
    0
    Views:
    100
Loading...

Share This Page