http://qs321.pair.com?node_id=1077596


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»