Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Count number of occurrence of word

by Kenosis (Priest)
on Mar 09, 2014 at 18:14 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1077596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found