in reply to Re^4: Pattern matching simultaneous substitution
in thread Pattern matching simultaneous substitution
If you mean "Note" by "Not", than your sample input is invalid, as both the sequences contain sDsDs.
Also, I think the following works correctly even for the non-alternating sequences in the way you originally showed:
It uses the bitwise or that doesn't short circuit to try both the substitutions every time while there's anything to replace.my %before = (U => 'I', D => 'O'); my %after; @after{ keys %before } = reverse values %before; sub subst { local ($_) = @_; 1 while s/(s+)(?=([DU]))/$before{$2} x length $1/e | s/(?<=([DU]))(s+)/$after{$1} x length $2/e; tr/DU/MM/ or tr/s/I/; return $_ }
Tested against:
use Test::More; my %simple = (ss => 'II', ssUUss => 'IIMMOO', ssDDss => 'OOMMII', ssDDssUUss => 'OOMMIIMMOO', ssUUssDDss => 'IIMMOOMMII'); for my $input (sort keys %simple) { is subst($input), $simple{$input}, $input; }
Might need some tweaking if the specification changes.
|
---|