Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Count number of occurrence of word

by Kenosis (Priest)
on Mar 09, 2014 at 18:14 UTC ( #1077596=note: print w/replies, xml ) Need Help??


in reply to Count number of occurrence of word

If you want to count all WORD occurrences within a string (but not within substrings), you can do the following using the goatse operator:

use strict; use warnings; my $string = 'How much wood could a woodchuck chuck if a woodchuck cou +ld chuck wood?'; my $woodCount = () = $string =~ /\bwood\b/g; print $woodCount; # prints 2

Hope this helps!

Replies are listed 'Best First'.
Re^2: Count number of occurrence of word
by fattahsafa (Sexton) on Mar 09, 2014 at 18:17 UTC
    Perfect ! Thanks a lot..it works

      You're most welcome!

      See also perlsecret for more fathomless stuff. I didn't know that too :-(

      Update: Fixed typo...

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1077596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2021-02-27 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?