in reply to Searching no. of times the pattern occurs in the data

hi agynr, you can make use of undef $/;, and replace all the pattern using g.

I have used $& which will take lots of memory and the process will be slow.To avoid that you can assign to another variable and replace with empty string instead of $&.

undef $/; my ($count) = $data =~ s/((a\s*bcd\s*e)(f\s*ghi)(jkl\s*tr))/$&/gsi;

Prasad

Replies are listed 'Best First'.
Re^2: Searching no. of times the pattern occurs in the data
by Roy Johnson (Monsignor) on Feb 12, 2005 at 15:04 UTC
    If you put the pattern in a lookahead and replace with nothing, you'll save CPU cycles:
    my $count = s/(?=$pat)//gsi;

    If there are no capturing parentheses in the expression, you can simply use the match operator instead.

    my $count = () = /$pat/gsi;

    Caution: Contents may have been coded under pressure.