kaatunut has asked for the wisdom of the Perl Monks concerning the following question:
or something like that. Cool. But s///eg runs out of shots pretty quickly: what if I want to mutilate areas before or or after the match, for example, to kill areas between two matches? What if I want the mutilated result to go through s///eg mangle too? So, I figure, I'll go like:$hop =~ s/(r(eg)e(xp)/mutilate($1,$2)/eg
while ($hop =~ m/r(eg)e(xp)/g) { // handle stuff // to replace, substr($hop,length $`,length $&) = $replace; and th +ink about slowness of $`, $& and all // to rewind, pos($hop)=$location; }
Now, I don't think we need do much benchmarks to see that the second version is slow as hell. What can I do about that? I realize $`, $& and calculating their lengths is incredibly slow, and I realize s/// has been optimized to do just that, but that doesn't help me a bit. Is there a way for s/// RHS to touch the output-string-in-build (I don't see any problems that could make)? Is there a way to know the location of LHS match in s/// RHS (again, I bet s/// already has this internally, didn't they just bother telling me?)? Any other suggestions?
P.S. Yes, this is related to my last Seek of Perl Wisdom. tilly, the approach you suggested I interpreted as way 2. If you didn't mean that, elaborate.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why is while(m//g){} so much slower than s///eg and what can I do about it?
by japhy (Canon) on Nov 18, 2000 at 21:52 UTC | |
|
Re (tilly) 1: Why is while(m//g){} so much slower than s///eg and what can I do about it?
by tilly (Archbishop) on Nov 18, 2000 at 22:16 UTC |