Replace Duplicate Digits in Every 9-digit Number in a Text File with Non-duplicate Ones
Problem description & analysis
Below is text file txt.txt:
853617639 975336865
The text file contains multiple numeric strings. Each string is 9-digit, and strings are separated by white space. We are trying to process each numeric string and output information according to the following rule: If a digit appears repeatedly, replace it with one within 1-9 that has not been appeared before (find the replacement in natural order). Below is the desired result:
853617249 975316824
Solution
Write the following script p1.dfx in esProc:
A |
|
1 |
=file("txt.txt").read() |
2 |
=A1.split(" ").(~.split@p()) |
3 |
>id=to(9) |
4 |
=A2.run(pos=~.group@p(~).(~.m(2:)).merge@u(),~(pos)=id\~) |
5 |
=A2.(~.concat()).concat(" ") |
6 |
=file("result.txt").write(A5) |
Explanation:
A1 Read the text file as a string.
A2 Split numeric strings into a sequence using the white space, and each member string into a sequence of integers.
A3 Define cellset variable id as integer sequence of 1-9.
A4 Loop through A2, during which cellset variable pos is defined to represent the position where a duplicate integer appears and find an integer from 1 to 9 that has not appeared in order to replace the duplicate.
A5 Concatenate member sequences of a sequence into a string.
A6 Write A5’s result to result.txt.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL