grep#

Filter a list of subjects in one call: the subjects the pattern matches, in their order.

Each element of \@subjects is matched once, from its start, in the pattern’s encoding. The options are name-value pairs:

  • indices => 1 - return the matching elements« indices instead of the elements.

  • captures => 1 - return, for each match, [subject, [$1, $2, ...]] (the index in place of the subject under indices). The list holds every group of the pattern, undef for one that did not take part, as strings in the pattern’s encoding.

  • skip_limited => 1 - a subject whose match reaches a limit (see new) counts as not matching, and limit_hit says how many there were. Without it such a subject croaks with the match error.

Without captures the returned elements are the array’s own scalars, as with Perl’s grep.

Synopsis#

my @hits = $re->grep(\@names);
my @at   = $re->grep(\@names, indices => 1, skip_limited => 1);
warn $re->limit_hit, " names skipped\n" if $re->limit_hit;
for ($re->grep(\@names, captures => 1)) {
    my ($name, $groups) = @$_;
    rename $name, "$groups->[1].$groups->[0]";
}