Selectively replace multiple instances of a character within a string: Difference between revisions

→‎{{header|Raku}}: Add a Ruku example
(→‎{{header|Raku}}: Add a Ruku example)
Line 21:
{{Template:Strings}}
<br>
 
=={{header|Raku}}==
Set up to not particularly rely on absolute structure of the word. Demonstrate with both the original 'abracadabra' and with a random shuffled instance.
 
<lang perl6>sub mangle ($str is copy) {
$str.match(:ex, 'a')».from.map: { $str.substr-rw($_, 1) = 'ABaCD'.comb[$++] };
$str.=subst('b', 'E');
$str.substr-rw($_, 1) = 'F' given $str.match(:ex, 'r')».from[1];
$str
}
 
say 'abracadabra', ' -> ', mangle 'abracadabra';
 
say $_, ' -> ', .&mangle given 'abracadabra'.comb.pick(*).join;</lang>
 
{{out}}
<pre>abracadabra -> AErBcadCbFD
caarabadrab -> cABraECdFDb</pre>
 
 
=={{header|Wren}}==
10,333

edits