in reply to Re: Counting words..
in thread Counting words..

Many thanks for the replies,

I got it working using $count++ while /$keyword/ig;

Printing the $count, then re-setting the counter (as I've got it in a loop to count each page searched).

I'm wrestling with searching for words/strings now (which is semi-working!), and will probably result in a new post!

Thanks again people,

NL

Replies are listed 'Best First'.
Re^3: Counting words..
by Roy Johnson (Monsignor) on Oct 24, 2005 at 17:11 UTC
    This may prove a bit more efficient:
    $count = () = /$keyword/ig;
    It puts the match into array context, which causes it to return the matches; then the resulting array is taken as a scalar, resulting in the count. This is a somewhat common Perl idiom. And you don't have to reset the counter.

    Of course, if you need to accumulate several counts, make it += instead of =, and do remember to reset the counter.


    Caution: Contents may have been coded under pressure.