On Thu, 2006-13-04 at 15:05 +0100, Max von Seibold wrote:
> I'm trying to write a small word counting script - I'm certain there are 
> zillions out there but it seemed a good learning exercise...
> 
> Bascially I read in each line from my text file which I want to count 
> with a basic while loop and file handle.
> 
> The problem is on the count. I know I could split the variable holding 
> the entire line on each whitespace and load all the components into an 
> array which I could subsequently count. However this seems a bit crude 
> and I was wondering if there is a better way to do with regular 
> expressions which processes each line as a batch.
> 
> 
> I know how to test for the occurrence of a 'word' with   
> 
> $lineFromFile =~ m/\s\w\s/i

my $count = () = $lineFromFile =~ m/\b\w+\b/;

In English:

  \b   # a word boundry
  \w+  # one or more alphanumeric characters

  () = $lineFromFile ...   # create a list of the matches

  $count = ()   # get the scalar() of the list

> 
> 
> However this only tells me if there are individual words in each line. 
> Is there some way I can count there occurrences? Something involving $1  ?


-- 
__END__

Just my 0.00000002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to