in reply to Re^4: Pattern matching simultaneous substitution
in thread Pattern matching simultaneous substitution

> Not that you cannot see s+D+s+D+s+ sequence, or s+U+s+U+s+. D and U alternate.

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:

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 $_ }
It uses the bitwise or that doesn't short circuit to try both the substitutions every time while there's anything to replace.

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.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]