Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: What's like $+ but not gives the ordinal?

by jima (Vicar)
on Jun 28, 2001 at 01:27 UTC ( #92069=note: print w/replies, xml ) Need Help??

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

You might also have luck with @-, brand-new to 5.6.0 (it says here in perlretut).
$-[0] is the offset of the start of the last successful match. $-[n] is the offset of the start of the substring matched by n-th subpattern, or undef if the subpattern did not match. for (qw(foo bar baz)) { if (/(foo)|(bar)|(baz)/) { print "\$+ = $+\n"; print "\@- = " . join('/',@-) . "\n"; print "scalar \@- = " . scalar(@-) . "\n"; } } __END__ $+ = foo @- = 0/0 scalar @- = 2 $+ = bar @- = 0//0 scalar @- = 3 $+ = baz @- = 0///0 scalar @- = 4
which would seem to indicate that you could do $sub[scalar(@-)-2]->();, although I think I'd prefer something easier to understand, if I were going to maintain this.

Replies are listed 'Best First'.
Re: What's like $+ but not gives the ordinal?
by Abigail (Deacon) on Jun 28, 2001 at 01:38 UTC
    That's a useless use of scalar. You could write $sub[@--2]->() too. ;-)

    -- Abigail

      Some of us always use scalar rather than implying it. It saves accidents and sometimes makes it clearer. Really, though, if I don't type it frequently I'll forget how to spell it.
        That's not very Perlish. I agree it can save some accidents, but really, on subtraction? And why just use scalar on one of the operands of -, and not the other? Wouldn't
        scalar ($sub [scalar (scalar (@-) - scalar (2))]) -> ()
        be more "always use scalar rather than implying it" then just using it once? Or do you perhaps mean you only use it sometimes?

        -- Abigail

Re: Re: What's like $+ but not gives the ordinal?
by John M. Dlugosz (Monsignor) on Jun 28, 2001 at 01:54 UTC
    Now that's something I never thought to try—the behavior of @+ and @- is totally different! (Hey Japhy, want to check out why?)

    I can put that into a function call, along with the other code in a comment, for maintainability. If a documented way arrives, easy to change. If it stops working, delete two lines and the other way takes over.


      This appears to be a bug. perlvar says that $#+ should return the number of successful groupings, but it doesn't -- it returns the number of attempted groupings. $#-, on the other hand, works as expected.

      japhy -- Perl and Regex Hacker
        My copy reads,

        This array holds the offsets of the ends of the last successful submatches in the currently active dynamic scope. $+[0] is the offset into the string of the end of the entire match. This is the same value as what the pos function returns when called on the variable that was matched against. The nth element of this array holds the offset of the nth submatch, so $+[1] is the offset past where $1 ends, $+[2] the offset past where $2 ends, and so on. You can use $#+ to determine how many subgroups were in the last successful match. See the examples given for the @- variable.
        It doesn't say that it tells you how many capturing parens were successful—rather, how many sets of capturing parens were present in the last successful match. That's consistant with its observed behavior.

        The docs for @- says that the array element will be undef if the capture paren did not match, which is not claiming that trailing undef entries are deleted from the array.

        I'd rather have a clear way to ask for either value than to use sideband information like this.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://92069]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-09-24 23:25 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (116 votes). Check out past polls.