You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When RA is given a List of indices as its second argument, it performs a replacement at each index. However, this can be problematic if a replacement is a different length from what it replaces.
Example: First, consider z RA [3 7] '_. This replaces the characters at indices 3 and 7 in the lowercase alphabet (d and h) with underscores. The result is abc_efg_ijklmnopqrstuvwxyz. But now, consider z RA [3 7] "___". This should replace the same two characters (d and h) with three underscores each; however, the replacement of d changes the length of the string, and now index 7 is no longer h but f: abc___e___ghijklmnopqrstuvwxyz. Similarly, z RA [3 7] "" should delete d and h but instead deletes i: abcefghjklmnopqrstuvwxyz.
The best solution is to sort the list of indices in descending order. By doing the rightmost replacements first, we can preserve the correct indices for characters to the left until we are ready to perform those replacements. Caveat: we'll need to handle indices that are Ranges, too, and I think it will be impossible to handle these perfectly because of the possibility of overlapping indices. TBD exactly how to sort when Ranges are involved.
The text was updated successfully, but these errors were encountered:
When
RA
is given a List of indices as its second argument, it performs a replacement at each index. However, this can be problematic if a replacement is a different length from what it replaces.Example: First, consider
z RA [3 7] '_
. This replaces the characters at indices 3 and 7 in the lowercase alphabet (d
andh
) with underscores. The result isabc_efg_ijklmnopqrstuvwxyz
. But now, considerz RA [3 7] "___"
. This should replace the same two characters (d
andh
) with three underscores each; however, the replacement ofd
changes the length of the string, and now index 7 is no longerh
butf
:abc___e___ghijklmnopqrstuvwxyz
. Similarly,z RA [3 7] ""
should deleted
andh
but instead deletesi
:abcefghjklmnopqrstuvwxyz
.The best solution is to sort the list of indices in descending order. By doing the rightmost replacements first, we can preserve the correct indices for characters to the left until we are ready to perform those replacements. Caveat: we'll need to handle indices that are Ranges, too, and I think it will be impossible to handle these perfectly because of the possibility of overlapping indices. TBD exactly how to sort when Ranges are involved.
The text was updated successfully, but these errors were encountered: