Counting words

F

Fran

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
 
W

Walter Roberson

: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.
 
G

Gunnar Hjalmarsson

Fran said:
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.
 
T

Tad McClellan

Fran said:
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
 
U

Uri Guttman

JE> Please define "word".

please define 'please'
please define 'define'

uri
 
J

J Krugman

In said:
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;
 
H

Helgi Briem

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__
 
D

David K. Wall

Uri Guttman said:
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);
}
 
B

Brad Baxter

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
 
W

Walter Roberson

:> 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!
 
B

Brad Baxter

:> 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.
 
B

Brad Baxter

<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
 
B

Brad Baxter

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top