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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How do I gather and output info about a file's contents?

Replies are listed 'Best First'.
Re: How do I gather and output info about a file's contents?
by mwah (Hermit) on Oct 13, 2007 at 11:45 UTC
    aureumI am very new to Perl and have been tasked to achieve the following using it.

    Who told you this? Why Perl?

    I have found solutions for every other scenario but this one


    Ohh fine! we'll come to this later (I have some problems in piping both directions to a process from within an Apache2/mod_perl process, maybe we can talk about this ...

    Open a text file after first checking it exists, otherwise 'die'.

    Always start your program by these two lines:
    use strict; use warnings;
    These will guide you through your development process. Then do a sequence of:
    my $fpath = '/somewhere/in'; my $fname = 'input.dat'; my $fn = "$fpath/$fname"; die "$fn: $!" unless -f $fn;
    which is what I'd consider "somehow idiomatic". The "test" is done by the -f
    operator (this has been adviced in another post).

    check the file and count all characters, including white space, words, sentences, paragraphs and lines used in the file.

    First, you have to read the file in one stroke (for this specific problem).
    It's called "file slurping", in Perl5 this is done in the lines of
    my $content; { open my $fh, '<', $fn or die "$fn: $!"; local $/; $content = <$fh>; }
    After this, your file is within the "$content" buffer. For counting
    I'd recommend a hash (but you can use whatever variable you want).
    Now we bump into the problem of how's your text "structured", what
    do you consider a "word", a "sentence" etc. My first guess would
    look like this:
    my %counts; $counts{CHARS} = length $content; $counts{WORDS} = scalar( () = $content =~ /\b/g ) / 2; $counts{SENTN} = scalar( () = $content =~ /\w\.\W/g ); $counts{PARAS} = scalar( () = $content =~ /\n/g ) + 1;
    The "CHARS" thing is trivial, its the length of the buffer
    holding the characters. The other counts are resolved by regular
    expressions in /g (catch all) mode in "list context", which is
    provided by the friendly goatse operator:  ... () = $content ...

    Then once it has done that it's to simply print the these returns to the screen like; characters = 'x' words = 'x' and so on.

    We can now loop over the "counter" hash and print out the results:
    for my $key (sort keys %counts) { print "$key ==> $counts{$key}\n" }
    This will print the counts according to the rules given
    by the regular expressions above.
    (Hope that helps to keep you going)

    Regards

    mwa
Re: How do I gather and output info about a file's contents?
by derby (Abbot) on Oct 13, 2007 at 11:36 UTC

    open and -X have the answers.

    -derby
Re: How do I gather and output info about a file's contents? (Golf)
by BrowserUk (Patriarch) on Oct 13, 2007 at 14:14 UTC

    Reproduce the output from wc using as few chars as possible. (634)

    Update: Put the space back.

    wc readme.txt 644 5611 36120 readme.txt perl -anlwe"$w+=@F;$c+=length}{print qq[\t$. $w ${\($c+$.*2)} $ARGV]" +readme.txt 644 5611 36120 readme.txt

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Low hanging fruit:

      perl -aplwe"$w+=@F;$c+=length}{$_=qq[\t$. $w ${\($c+$.*2)} $ARGV]"

      Updated: There seems to be a space missing before $ARGV

Re: How do I gather and output info about a file's contents?
by apl (Monsignor) on Oct 13, 2007 at 12:16 UTC
    It's bad manners to insult our intellegence before you ask us to do your homework for you.

    I have been researching this for months now

    If you've taken months, read four books, and still haven't stumbled across the print statement, you should consider another line of work.

      Well thanks for that! Please read my question then you'll see I've not asked anything of the sort!!!!!!
        I see that you modified the question, ttaking out the references to sepending months on this, which four books you've used, how you were "assigned" this task, etc.

        I wouldn't have commented as I did if you had shown the code you had written, explaining specifically what you had tried and what problem you were encountering.

        For that mater, you still haven't explained what you mean by how to gather the statistics in a text file,

Re: How do I gather and output info about a file's contents?
by ysth (Canon) on Oct 14, 2007 at 03:12 UTC
    I don't have my Programming Perl handy, but doesn't the very first example in it deal with gathering statistics in a text file?
A reply falls below the community's threshold of quality. You may see it by logging in.