Selectively replace multiple instances of a character within a string: Difference between revisions
Content added Content deleted
(add sed) |
(Added Quackery.) |
||
Line 773: | Line 773: | ||
main()</syntaxhighlight> |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>AErBcadCbFD</pre> |
|||
=={{header|Quackery}}== |
|||
If there is no nth instance of a particular character in the string, <code>selectivereplace</code> will disregard it. For example, if the nest of replacements in the example included <code>[ 1 char z char ! ]</code> the output would be the same, as there is no z in "abracadabra". |
|||
<syntaxhighlight lang="Quackery"> [ dup size temp put |
|||
witheach |
|||
[ over = if |
|||
[ swap 1 - |
|||
tuck 0 = if |
|||
[ i^ temp replace |
|||
conclude ] ] ] |
|||
2drop |
|||
temp take ] is nfind ( n c $ --> n ) |
|||
[ temp put |
|||
[] [] rot witheach |
|||
[ 2 pluck |
|||
dip rot join unrot |
|||
nested join ] |
|||
[] swap witheach |
|||
[ do |
|||
temp share nfind |
|||
join ] |
|||
temp take 0 join unrot |
|||
witheach |
|||
[ dip [ behead rot ] |
|||
poke swap ] |
|||
drop |
|||
-1 split drop ] is selectivereplace ( [ $ --> $ ) |
|||
' [ [ 1 char a char A ] |
|||
[ 2 char a char B ] |
|||
[ 4 char a char C ] |
|||
[ 5 char a char D ] |
|||
[ 1 char b char E ] |
|||
[ 2 char r char F ] ] |
|||
$ "abracadabra" |
|||
selectivereplace echo$</syntaxhighlight> |
|||
{{out}} |
|||
<pre>AErBcadCbFD</pre> |
<pre>AErBcadCbFD</pre> |
||