[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Q on Substitution improvement



>>>>> "L" == L  <Brian> writes:

L> Randal L. Schwartz writes:
L> |    $map{'"'} = "&quot;";
L> |    $map{"\n"} = "<br>";
L> |    ...
L> |    my $pat = "(" . join("|", map quotemeta $_, reverse sort keys %map), ")";
L> |    eval q{$pat = qr/$pat/} if $] >= 5.005;
L> |    s/$pat/$map{$1}/g;

L> Maybe it's way too early in the morning, but why "reverse sort keys %map"
L> and not just "keys %map"?

Did you read my comment below?  I want to match \n\n before \n, so I need
the longer strings first.

L> Also, does the qr/$pat/ do anything useful in this case? s/// would have to
L> do it anyway, and being there's only one substitution, it seems it would
L> still happen only once without the eval. If there were multiple s/// with
L> $pat as the expression to match I can see a distinct advantage, but not here.

If the mapping and $pat-ing are used in multiple places, it makes
sense to compile the regex.  Yes, if used once, no. :)


-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org