in reply to Re^2: map function use
in thread map function use

The following works:

use strict; use warnings; my @phrases=('(arf)71', 'p65(91)', 'H:223', 'Phospholipase A2 inhibito +r', 'Phospholipase A2', 'Phospholipase', 'PLI-A', 'PLI-B', 'PLI'); my $string = join '|', map { quotemeta } @phrases; my $in = "(arf)71 p65(91) H:223 Phospholipase A2 inhibitor ( PLI ) , p +urified from the blood plasma of the Habu snake ( Trimeresurus flavov +iridis ) , was separated into two distinct subunits , PLI-A and PLI-B +"; my $out = "#(arf)71# #p65(91)# #H:223# #Phospholipase A2 inhibitor# ( +#PLI# ) , purified from the blood plasma of the Habu snake ( Trimeres +urus flavoviridis ) , was separated into two distinct subunits , #PLI +-A# and #PLI-B#"; #while($in =~ s/(^|\s)($string)(\s|$)/$1#$2#$3/) {}; while($in =~ s/(^|\s)($string)(\s|$)/$1#$2#$3/g) { pos() = pos() - 1; +}; print "success!!\n" if($in eq $out); print "in: $in\n"; print "out: $out\n";

Replies are listed 'Best First'.
Re^4: map function use
by ikegami (Patriarch) on Aug 13, 2009 at 22:29 UTC
    pos() = pos() - 1;? ow! Use zero-width matches.
    my $phrases_pat = join '|', map quotemeta, @phrases; $in =~ s/(?<!\S)($phrases_pat)(?!\S)/#$1#/g;

    Bonus: In addition to being much simpler, this should be faster since much less work is outside the substitution, in matching and in replacing.