in reply to Counting words..

This will also work for counting words (aka occurences of a pattern in a string):
#!/usr/bin/perl use strict; use warnings; read DATA, my $text, 40; ### this is the important line ### my $wordcount = () = $text =~ /test/gi; print $wordcount; __DATA__ TEST tester testing asdf lalala greatest

Update: The while++ solution previously posted takes half the time to run as this one. Can anyone explain why?

Replies are listed 'Best First'.
Re^2: Counting words..
by Roy Johnson (Monsignor) on Oct 24, 2005 at 17:41 UTC
    How depressing; I suggested it was more efficient. My guess is that building the list (which isn't used, after all) takes more time than repeatedly incrementing a counter.

    Caution: Contents may have been coded under pressure.