Counting words

Discussion in 'Perl Misc' started by Fran, Feb 4, 2004.

  1. Fran

    Fran Guest

    Hi,

    Can anybody please tell me how to count words? If a need to know if an
    input has n words, how do I do it? Thank you
     
    Fran, Feb 4, 2004
    #1
    1. Advertising

  2. In article <>,
    Fran <> wrote:
    :Can anybody please tell me how to count words? If a need to know if an
    :input has n words, how do I do it? Thank you

    What's a "word" for this purpose? How many words on the line

    123.45e+73,nifty is it ? "Hello, ... . I'm fine" she said.


    --
    Look out, there are llamas!
     
    Walter Roberson, Feb 4, 2004
    #2
    1. Advertising

  3. Fran wrote:
    > Can anybody please tell me how to count words? If a need to know if
    > an input has n words, how do I do it?


    That's a FAQ; see perlfaq4:

    "How can I count the number of occurrences of a substring within a
    string?"

    > Thank you


    You are welcome.

    But please note that you are supposed to check the FAQ before posting
    a question here.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 4, 2004
    #3
  4. Fran wrote:
    > Can anybody please tell me how to count words? If a need to know if an
    > input has n words, how do I do it?


    Please define "word".

    jue
     
    Jürgen Exner, Feb 5, 2004
    #4
  5. Fran <> wrote:

    > Can anybody please tell me how to count words?



    I don't know what your original problem is,
    but I suggest to use a hash. --Rafael Garcia-Suarez


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 5, 2004
    #5
  6. Fran

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    JE> Fran wrote:
    >> Can anybody please tell me how to count words? If a need to know if an
    >> input has n words, how do I do it?


    JE> Please define "word".

    please define 'please'
    please define 'define'

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 5, 2004
    #6
  7. Fran

    J Krugman Guest

    In <> (Fran) writes:

    >Can anybody please tell me how to count words? If a need to know if an
    >input has n words, how do I do it? Thank you


    I think the simplest thing to do is to use split (perldoc -f split)
    and count the number of elements in the result. The main difficulty
    is to find the right pattern to split on... Maybe this would work
    for you:

    @w = split /\s|\W{2,}/, $string; # check the split?
    $n = scalar @w;
     
    J Krugman, Feb 5, 2004
    #7
  8. Fran

    Helgi Briem Guest

    On 4 Feb 2004 14:04:29 -0800, (Fran) wrote:


    >Can anybody please tell me how to count words? If a need to know if an
    >input has n words, how do I do it? Thank you


    Well, as others have so readily told you, it's fairly
    hard to define a word. As long as you don't mind
    a haphazard definition (words are separated by whitespace),
    something like this will do the trick:

    #!perl
    use warnings;
    use strict;
    my $totalwords = 0;
    while (<>)
    {
    my @words = split;
    $totalwords += @words;
    }
    print $totalwords;
    __END__
     
    Helgi Briem, Feb 5, 2004
    #8
  9. Uri Guttman <> wrote:

    >>>>>> "JE" == Jürgen Exner <> writes:

    >
    > JE> Fran wrote:
    > >> Can anybody please tell me how to count words? If a need to know if
    > >> an input has n words, how do I do it?

    >
    > JE> Please define "word".
    >
    > please define 'please'
    > please define 'define'


    Ok, Mr. Turtle... ITYM:


    define('please', 'define');

    sub define {
    my @more;
    for my $wp (@_) {
    my $t = "Please define '$wp'";
    print $t . "\n";
    push @more, $t;
    }
    define(@more);
    }
     
    David K. Wall, Feb 5, 2004
    #9
  10. Fran

    Brad Baxter Guest

    On Wed, 4 Feb 2004, Fran wrote:
    > Can anybody please tell me how to count words? If a need to know if an
    > input has n words, how do I do it? Thank you


    perl -pale '$=+=@F}{*_=*=' input

    Regards,

    Brad
     
    Brad Baxter, Feb 6, 2004
    #10
  11. In article <>,
    Brad Baxter <> wrote:
    :On Wed, 4 Feb 2004, Fran wrote:
    :> Can anybody please tell me how to count words? If a need to know if an
    :> input has n words, how do I do it? Thank you

    :perl -pale '$=+=@F}{*_=*=' input

    That's cruel!
    --
    IEA408I: GETMAIN cannot provide buffer for WATLIB.
     
    Walter Roberson, Feb 6, 2004
    #11
  12. Fran

    Brad Baxter Guest

    On Thu, 6 Feb 2004, Walter Roberson wrote:

    > In article <>,
    > Brad Baxter <> wrote:
    > :On Wed, 4 Feb 2004, Fran wrote:
    > :> Can anybody please tell me how to count words? If a need to know if an
    > :> input has n words, how do I do it? Thank you
    >
    > :perl -pale '$=+=@F}{*_=*=' input
    >
    > That's cruel!


    Flatterer.
     
    Brad Baxter, Feb 6, 2004
    #12
  13. Fran

    Brad Baxter Guest

    Shameless plug

    <warning content="baby pictures">

    For the last 10 years I've been programming for the (U.S.) state of
    Georgia's virtual library system, GALILEO.

    http://www.galileo.usg.edu

    This is where the majority of my Perl (and Web) misdemeanors are
    committed. It's main function is to provide access to licensed resources,
    so you'll likely only be able to see the public databases, notably those
    in the Digital Library of Georgia.

    http://www.galileo.usg.edu/express?link=dlg1

    Our newest arrival there is Georgia Aerial Photographs.

    http://www.galileo.usg.edu/express?link=gaph

    Not much Perl visible there; the static pages are pregenerated nightly.
    Short-range plans: fix all the mistakes; make the ASCS photomosaics
    zoomable, too; integrate with gazetteer place data. Longer-range wishes:
    scan the rest of the air photos; implement true GIS access.

    </warning>

    I'm grateful to all the Perl developers for making what I do easy enough
    that I can do it, that my employers remain willing to bankroll my
    programming habit, and that my wife hasn't murdered me in my sleep.

    Thank you for indulging a proud father.

    Regards,

    Brad
     
    Brad Baxter, Feb 6, 2004
    #13
  14. Brad Baxter wrote:
    >
    > On Thu, 6 Feb 2004, Walter Roberson wrote:
    >
    > > In article <>,
    > > Brad Baxter <> wrote:
    > > :On Wed, 4 Feb 2004, Fran wrote:
    > > :> Can anybody please tell me how to count words? If a need to know if an
    > > :> input has n words, how do I do it? Thank you
    > >
    > > :perl -pale '$=+=@F}{*_=*=' input
    > >
    > > That's cruel!

    >
    > Flatterer.


    Unfortunately the count will be off by 60 because $= starts with the
    value 60.

    perl -pale '$=+=@F}{$_=$=-60' input


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Feb 7, 2004
    #14
  15. Fran

    Brad Baxter Guest

    On Sat, 7 Feb 2004, John W. Krahn wrote:

    > Brad Baxter wrote:
    > > On Thu, 6 Feb 2004, Walter Roberson wrote:
    > > > In article <>,
    > > > Brad Baxter <> wrote:
    > > > :perl -pale '$=+=@F}{*_=*=' input
    > > > That's cruel!

    > > Flatterer.

    >
    > Unfortunately the count will be off by 60 because $= starts with the
    > value 60.
    >
    > perl -pale '$=+=@F}{$_=$=-60' input


    Too true. I should have stopped here and been happy.

    perl -pale '$n+=@F}{*_=*n' input

    My apologies to the OP.

    Brad
     
    Brad Baxter, Feb 7, 2004
    #15
    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. Francois
    Replies:
    13
    Views:
    615
    Will Hartung
    Oct 14, 2004
  2. sun6
    Replies:
    4
    Views:
    643
    Michael Str.
    Feb 22, 2004
  3. Ksenia Marasanova

    extracting HTML fragments and counting words

    Ksenia Marasanova, Feb 18, 2005, in forum: Python
    Replies:
    0
    Views:
    298
    Ksenia Marasanova
    Feb 18, 2005
  4. Travers Naran
    Replies:
    4
    Views:
    378
    John Machin
    May 25, 2005
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    204
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page