http://www.perlmonks.org?node_id=1018575

evereno has asked for the wisdom of the Perl Monks concerning the following question:

I have been asked to input a text file ,, I have to display a number of lines and a number of words, display the longest and shortest word in each line , occurrencess of words in each document how do I do this please

Replies are listed 'Best First'.
Re: Perl beginnere
by kennethk (Abbot) on Feb 13, 2013 at 17:44 UTC
    Some useful starting information:
    1. Files and I/O in perlintro talks about how to open and read a file.
    2. You need to define what you mean by a 'word'. I know that sounds a bit strange at first, but that will control how you split your sentences up.
    3. length and split will probably be very useful functions, though you may use other variants on regular expressions for the line parsing.
    4. For all word counts, you'll definitely want to use a hash.

    Post code that you've written, and we'll be happy to give both syntactic and stylistic feedback. However, we're here to help you learn, and learning generally takes a lot of work on your part. See How do I post a question effectively?.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Perl beginnere
by choroba (Cardinal) on Feb 13, 2013 at 17:00 UTC
    Welcome to the Monastery, evereno.

    Did not the person who asked you to do that also give you some materials to study?

    What have you tried? What did not work?

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl beginner
by toolic (Bishop) on Feb 13, 2013 at 17:18 UTC
    • Start by reading perlintro.
    • Write some code.
    • Ask specific questions related to the code you have tried.
Re: Perl beginnere
by sundialsvc4 (Abbot) on Feb 13, 2013 at 20:22 UTC

    Start by surfing for examples.   Start with the very simplest one:   a loop that reads a file and prints each line.   From there, you can see your way to how to count the lines.   Next, look at split, and write a separate experimental script that takes some string and splits it.   Figure out how to count the number of elements in that string, and how to write a loop to iterate through the list.   You can find examples of all of these things ... and this is the only way to learn how to do this stuff.   You’re going to stumble, you’re going to make mistakes, and you’re going to learn the craft from that.

Re: Perl beginnere
by blue_cowdawg (Monsignor) on Feb 13, 2013 at 20:44 UTC

    I wonder who the professor is because someone else asked me the same question almost word for word and here was my answer (external site) Equivalent for the *nix "wc" command)


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg