Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Count number of occurrence of word

by fattahsafa (Sexton)
on Mar 09, 2014 at 17:59 UTC ( [id://1077592]=perlquestion: print w/replies, xml ) Need Help??

fattahsafa has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How can I count number of occurrence of a WORD in a string? word doesn't include sub string. Thanks, Abed.

Replies are listed 'Best First'.
Re: Count number of occurrence of word
by Kenosis (Priest) on Mar 09, 2014 at 18:14 UTC

    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!

      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»

Re: Count number of occurrence of word
by Lennotoecom (Pilgrim) on Mar 09, 2014 at 18:29 UTC
    use Benchmark(cmpthese); cmpthese(100_000, { 'a' => sub { $_ = 'this that this those this these.'; $a = () = /this/g; }, 'b' => sub { $_ = 'this that this those this these.'; $a++ while(s/this//); } });
    Rate a b a 458716/s -- -7% b 492611/s 7% --
    wrong?
      Hi, the strange thing is that, replicating your benchmark (together with a third solution), I get a much higher advantage for the 'a' solution than what you got:
      $ perl -e 'use Benchmark(cmpthese); cmpthese(1000000, { "a" => sub { $_ = "this that this those this these."; $a = () = /this/g; }, "b" => sub { $_ = "this that this those this these."; $a++ while(s/this//); }, "c" => sub { $_ = "this that this those this these."; $a = s/this/this/g; } });' Rate a b c a 282406/s -- -28% -28% b 390778/s 38% -- -1% c 393391/s 39% 1% --
      Note that I had to use one million iterations, with 100_000 I got the "too few iterations" warning.
        ummm you mean a disadvantage?
        use Benchmark(cmpthese); cmpthese(1000000, { 'a' => sub { $_ = 'this that this those this these.'; $a = () = /this/g; }, 'b' => sub { $_ = 'this that this those this these.'; $a++ while(s/this//); }, 'c' => sub { $_ = 'this that this those this these.'; $a = s/this/this/g; } });
        my machine shows that your 'c' option is the fastest of these
        Rate a b c a 478240/s -- -4% -19% b 497018/s 4% -- -16% c 588235/s 23% 18% --
Re: Count number of occurrence of word
by Laurent_R (Canon) on Mar 09, 2014 at 18:06 UTC
    Hi, try this:
    my $count = $string =~ s/word/word/g;
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1077592]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-26 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found