John, this stuff gets a bit tricky because what I've done is write a code generator.

I don't understand why you need to localize the backref variables.

Because if the match fails on the current attempt, but it succeeded on a previous attempt, the backref variables ($1, $2, etc) will contain the values from the previous match. The following code demonstrates this:

my $string = '1234'; $string =~ /(2)/; $string =~ /(a)/; print $1;
Also, you're finding the first capture, not the last.

I misread your post then. Make the following change:

- for ( 1 .. $num_refs ) { + for ( $num_refs .. 1 ) {
Here is a way that finds the max by itself...

I didn't know about the @+ variable :)

In your new code, you are localizing the same name as you are my-ing.

Actually, I'm not. Before the eval statement, add the statement print $code;. That will show you what's going on. The HERE document is a scalar containing generated code to be evaled. That's all.

As I mentioned, my code is probably not worth the effort as I did not know about @+. :)

Update 2: Duh! Of course $num_refs .. 1 isn't going to work. Sigh.

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re(3): What's like $+ but not gives the ordinal? by Ovid
in thread What's like $+ but not gives the ordinal? by John M. Dlugosz

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.