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

Line 355:
{{Out}}
<pre>AErBcadCbFD</pre>
 
=={{header|jq}}==
In this section, array-indexing and occurrence-counting are both 0-based except for the transcription of the task in `steps`.
 
'''Generic functions'''
<syntaxhighlight lang="jq">
# Emit empty if the stream does not have an $n-th item
# Note: jq's nth/2 does not serve our purposes.
def n_th($n; stream):
if $n < 0 then empty
else foreach stream as $x (-1; .+1; if . == $n then $x else empty end)
end;
 
def positions(stream; $v):
foreach stream as $x (-1; .+1; if $v == $x then . else empty end);
 
# Input: an array or string.
# Output: the input with an occurrence of $old replaced by $new.
# . and $reference are assumed to be of the same type and length.
# The search occurs in $reference and the corresponding spot in . is modified.
def replace_nth($occurrence; $old; $new; $reference):
if type == "array"
then ($reference | n_th($occurrence; positions(.[]; $old)) // null) as $ix
| if $ix then .[:$ix] + [$new] + .[$ix + 1:] else . end
else explode
| replace_nth($occurrence; $old|explode|first; $new|explode|first; $reference|explode)
| implode
end;
</syntaxhighlight>
 
'''The task'''
<syntaxhighlight lang="jq">
def steps:
[1, "a", "A"],
[2, "a", "B"],
[4, "a", "C"],
[5, "a", "D"],
[1, "b", "E"],
[2, "r", "F"];
 
def task(steps):
. as $reference
| reduce steps as [$occurrence, $old, $new] (.;
replace_nth($occurrence - 1; $old; $new; $reference ));
"abracadabra" | task(steps)
</syntaxhighlight>
{{Output}}
<pre>
AErBcadCbFD
</pre>
 
=={{header|Julia}}==
2,442

edits