in reply to Re: (Ovid) Re: What's like $+ but not gives the ordinal?
in thread What's like $+ but not gives the ordinal?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Ovid) Re(3): What's like $+ but not gives the ordinal?
by John M. Dlugosz (Monsignor) on Jun 28, 2001 at 02:25 UTC |