Copy The Other Rows in a Text File the Times Which Is the Number in the Last Row
Problem description & analysis
We have a txt file txt.txt, as shown below:
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
3
The following text file has N+1 rows, in which row (N+1) contains a number M, say 3. We are trying to generate a new file according to this file. The rule is that repeat row N M times and get an altogether N*M rows, as shown below:
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
Solution
Write the following script p1.dfx in esProc:
A |
|
1 |
=file("txt.txt").import@i() |
2 |
=A1.m(:-2)*int(A1.m(-1)) |
3 |
=file("result.txt").export(A2) |
Explanation:
A1 Import the txt data. @i option enables returning a sequence if the result set contains only one column.
A2 Copy the sequence from which the last member is removed the integer times which is value of the last member.
A3 Export A2’s result to result.txt.
Find how to integrate the script code with a Java program in How to Call an SPL Script in Java.
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