in reply to Printing the count for regex matches

How's this? I used (?:) to shorten the regex; and parens in the join to get rid of the dangling comma.

use strict; use warnings; my @matches; while (<DATA>) { push @matches, $1 if /((?:first|second|third|fourth|fifth)_string)/ +; } print scalar @matches, " matches found: ", join(", ", @matches), "\n";

Replies are listed 'Best First'.
Re^2: Printing the count for regex matches
by CountZero (Bishop) on Mar 10, 2009 at 05:49 UTC
    Nice try, but what will happen if you have more than one string matching in one line? You will only catch the first one and miss all subsequent ones.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James