stratkid has asked for the wisdom of the Perl Monks concerning the following question:
$_ = "lothlorien"; m/ (?{ $i = 0 }) (. (?{ local $i = $i + 1; }) )* lori (?{ $result = $i }) /xi;
Now i = 4.
This example purportedly makes use of dynamic scoping. Unfortunately, the Camel has done a pretty crappy job explaining, or even using in examples, things like "local". The example right before this one was the exact same thing, but just used "$i++" instead of local, and didn't need the $result variable at the end. In that one, $i=10. A fellow monk explained to me that the .* greedily gobbles up the whole string, 10 characters, so $i gets incremented to 10 and the .* never matches again. (it only gives up some characters due to back-tracking). But in this one $i is 4!
I have no idea why, and my study has come to a grinding halt. Please help! Thanks :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: reg-ex frustration
by japhy (Canon) on May 04, 2002 at 06:24 UTC | |
|
Re: reg-ex frustration
by tachyon (Chancellor) on May 04, 2002 at 06:02 UTC | |
|
Re: reg-ex frustration
by neilwatson (Priest) on May 04, 2002 at 14:16 UTC |