in reply to Separating multiple keyword search input
Don't do several substitutions one after another, you could end up replacing what you replaced, again. For example:$main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g; $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%premiumlistings%%/$premiumitem/g;
prints "gang".$_ = 'taxial'; s/taxi/cab/g; s/cabal/gang/g; print;
Better would be to replace both at once:
which prints "cabal". Once the first substitution went past "taxi", replacing it with "cab", we've moved too far to replace it again in the second substitution. Usually, this is exactly what we want.$_ = 'taxial'; my %subst = ( 'taxi' => 'cab', 'cabal' => 'gang' ); local $" = "|"; my @keys = map quotemeta, sort { length $b <=> length $a } keys %sub +st; my $regexp = qr(@keys); s/($regexp)/$subst{$1}/g; print;
I like Regex::PreSuf (abbreviated from prefix/suffix) for this kind of solution. It combines several "words" into one regexp, as alternatives, and takes care of quotemeta as well. The code is shorter, and may likely become faster, too. That's the idea, though it depends on the words in the list.
$_ = 'taxial'; my %subst = ( 'taxi' => 'cab', 'cabal' => 'gang' ); use Regex::PreSuf; my $regexp = presuf(keys %subst); s/($regexp)/$subst{$1}/g; print;
Applied to your code, it becomes:
BTW Using the /o modifier, the regexp is compiled only once, the first time a match is tried. Even though you can later change the values in the substitution hash, adding more keys will not do any good.my %subst = ( %fields, searcresults => $pitem, premiumlistings => $premiumitem ); use Regex::PreSuf; my $regexp = presuf(keys %subst); $main_template =~ s/%%($regexp)%%/$subst{$1}/go;
|
|---|