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

Content added Content deleted
(Added Go)
(Added Perl)
Line 56: Line 56:
</pre>
</pre>


=={{header|Perl}}==
<lang perl>use strict;
use warnings;
use feature 'say';

sub transmogrify {
my($str, %sub) = @_;
for my $l (keys %sub) {
$str =~ s/$l/$_/ for split '', $sub{$l};
$str =~ s/_/$l/g;
}
$str
}

my $word = 'abracadabra';
say "$word -> " . transmogrify $word, 'a' => 'AB_CD', 'r' => '_F', 'b' => 'E';</lang>
{{out}}
<pre>abracadabra -> AErBcadCbFD</pre>
=={{header|Raku}}==
=={{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.
Set up to not particularly rely on absolute structure of the word. Demonstrate with both the original 'abracadabra' and with a random shuffled instance.