in reply to What's like $+ but not gives the ordinal?

You could do kludge in your regex:
if ($string =~ /(foo(?{ $N = 1 }))|(bar(?{ $N = 1 }))/) { if ($N == 1) { ... } else { ... } }
Or you could do:
if (my @capt = $string =~ /(foo)|(bar)/) { $N = @capt; for (reverse @capt) { last if defined; $N--; } if ($N == 1) { ... } else { ... } }
Since I seem to be (becoming) the regcomp.c pumpking, I'll see if I could kludge something together.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: What's like $+ but not gives the ordinal?
by John M. Dlugosz (Monsignor) on Jun 28, 2001 at 01:15 UTC
    I thought about using (?{$N=1}) etc. in each branch, but shy away from the "highly experimental" feature that may be changed without notice.

    The if to the array is interesting, but won't work for me because I'm doing a substitution, not just a match. The docs give an /G../gc idiom for lexing, but there is no equivilent for altering the parts not just detecting them.

    As for kludgeing something together, since we're getting away from funny globals altogether, how about making a bold move and providing a variable named $Regexp::last_paren_match_count, or better yet @Regexp::matches that aliases the same values as $1, $2, etc. then they can be manipulated as an array in all the normal ways, index with -1, get array length, etc.

    —John