Selectively replace multiple instances of a character within a string: Difference between revisions
Content added Content deleted
(Add Factor) |
m (julia example) |
||
Line 86: | Line 86: | ||
AErBcadCbFD |
AErBcadCbFD |
||
</pre> |
</pre> |
||
=={{header|Julia}}== |
|||
<lang ruby> |
|||
rep = Dict('a' => Dict(1 => 'A', 2 => 'B', 4 => 'C', 5 => 'D'), 'b' => Dict(1 => 'E'), 'r' => Dict(2 => 'F')) |
|||
function trstring(string, repdict) |
|||
seen, newchars = Dict{Char, Int}(), Char[] |
|||
for c in string |
|||
i = get!(seen, c, 1) |
|||
push!(newchars, haskey(repdict, c) && haskey(repdict[c], i) ? repdict[c][i] : c) |
|||
seen[c] += 1 |
|||
end |
|||
return String(newchars) |
|||
end |
|||
trstring("abracadabra", rep) |
|||
<lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |