Below is a better reduction of the original code that gave me a problem and started me down this road.

I am not quite sure what you mean by I believe that your for loop should loop over (1..$#-)? As opposed to 1..$#+?

perlvar says:

You can use $#- to determine how many subgroups were in the last successful match

and You can use $#+ to determine how many subgroups were in the last successful match

Doesn't this mean they should be the same?

Update:I just found this in Japhy's book...

The last index of @-, $#-, is the number of the last matching back-reference (and so can be used to find out which $DIGIT variable $+ corresponds to), whereas the last index of @+, $#+, is the number of back-references, defined or not

Which explains things somewhat. And there was I just getting comfortable with the $#array idiom:(

End update

With regard to ${1} eq $1....see below, using $1 in the first print inside the for loop gives "abc". Using ${$_} (where $_ = 1) results in the error?

I'd assume this was a throw-over from pre-strict Perl days except that (from what I read) @- & @+ didn't arrive until 5.6.

As for what I was trying to acheive.... I am only slightly embarassed to admit that I thought that the @- & @+ arrays would give me the start/stop indexs for each captured match when used with the //g option. I now see that this was wrong, but that is why I am playing with regexs...I'm attempting to understand what they can do (and how). It saddens and confuses me slightly that someone actually bothered to downvote my original question. I always thought that the only bad question was the one you didn't ask. C'est la vie.

#! perl -w use strict; my $haystack = "abcdefghijklmnopqrstuvwxyz"; my @needles = qw(abc cde fgh ghi ijk pqr rst xyz); my $regex = '(' . join('|', @needles ) . ')'; print "Pre-compilation:", $regex,$/; $regex = qr/$regex/s; print "Post-compilation:", $regex, $/; $haystack =~ /$regex/g; if ($#-) { print "$#+ matches found", $/; for (1..$#+) { print "\$_ is $_, & \$1 is $1", $/; print "$haystack contains ${$_} at $-[$_]", $/; } } __END__ C:\test>test Pre-compilation:(abc|cde|fgh|ghi|ijk|pqr|rst|xyz) Post-compilation:(?s-xim:(abc|cde|fgh|ghi|ijk|pqr|rst|xyz)) 1 matches found $_ is 1, & $1 is abc Can't use string ("1") as a SCALAR ref while "strict refs" in use at C +:\test\test.pl line 19. C:\test>

In reply to Re: Re: Using @- & @+ by BrowserUk
in thread Using @- & @+ by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.