in reply to empty hits with regex

You have two capturing parens in the pattern, so the match operator returns two captures when the match operator is successful. Always.

Here's a couple of other solutions:

my @hits = grep defined, m/(?:c(a)|k(t))/g;
my @hits; while ( m/(?:c(a)|k(t))/g ) { push @hits, defined $1 ? $1 : $2; }

The last one is extensible too:

my @hits; while ( m/(?:c(a)|k(t)|k(k))/g ) { push @hits, defined $1 ? $1 : defined $2 ? $2 : $3 }

Replies are listed 'Best First'.
Re^2: empty hits with regex
by JavaFan (Canon) on Oct 02, 2008 at 20:38 UTC
    In 5.10, there's a much easier solution: (?|).
    $_ = "caaat"; my @hits = /(?|c(a)|k(t))/g; say scalar @hits; say @hits; __END__ 1 a

      You just made me want to get our infrastructure team to upgrade to perl 5.10. Thanks. Now I'm not going to get any real work done next week.