Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions
m (changed wording of the (draft) task, added a requirement to show) |
|||
Line 20: | Line 20: | ||
Also, don't include the rightmost informative comments (◄■■■■■■), as they are not meant to be part of the file. |
Also, don't include the rightmost informative comments (◄■■■■■■), as they are not meant to be part of the file. |
||
<br><br> |
<br><br> |
||
=={{header|Nim}}== |
|||
We provide a procedure which takes input and output files as parameters. |
|||
Assumptions are the following: |
|||
– a line is a sequence of bytes terminated by CR, LF or CR-LF; |
|||
– there is enough memory to process the file in memory i.e to store the input file as a string, the sequence of lines, the reverse sequence of lines and the output file as a string. |
|||
<lang Nim>import algorithm, strutils |
|||
proc reverseLines(infile, outfile: File) = |
|||
let lines = infile.readAll().splitLines(keepEol = true) |
|||
outfile.write reversed(lines).join("") |
|||
when isMainModule: |
|||
reverseLines(stdin, stdout)</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
Revision as of 15:53, 3 August 2021
- Task
Reverse the order of the lines (records) of an entire (input) file.
Show the results here, on this page.
For the input file, use the following five lines (records):
"Diplomacy is the art of ◄■■■■■■ starts in column 3. saying 'Nice Doggy' ◄■■■■■■ starts in column 5, until you can find a rock." ◄■■■■■■ starts in column 2, ◄■■■■■■ (a blank line), --- Will Rodgers ◄■■■■■■ starts in column 30.
You can (or may) assume there are no trailing blanks, and that line four has one blank.
Also, don't include the rightmost informative comments (◄■■■■■■), as they are not meant to be part of the file.
Nim
We provide a procedure which takes input and output files as parameters. Assumptions are the following:
– a line is a sequence of bytes terminated by CR, LF or CR-LF;
– there is enough memory to process the file in memory i.e to store the input file as a string, the sequence of lines, the reverse sequence of lines and the output file as a string. <lang Nim>import algorithm, strutils
proc reverseLines(infile, outfile: File) =
let lines = infile.readAll().splitLines(keepEol = true) outfile.write reversed(lines).join("")
when isMainModule:
reverseLines(stdin, stdout)</lang>
Raku
Simplest thing that could possibly satisfy the extremely vague task description and completely glossing over all of the questions raised on the discussion page.
ASSUMPTIONS:
- File is redirected into STDIN from command line.
- Is a Unix or Windows format text file.
- Is in UTF8 encoding or some subset thereof.
- May hold entire file in memory.
<lang perl6>.put for reverse lines</lang>
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Input file lines:" + nl
fp = fopen("..\New\text.txt","r") r = "" txt = "" while isstring(r)
r = fgetc(fp) if r = -1 loop ok if r = char(10) txt += nl else txt += r ok
end
see txt + nl see "Reversed file lines: " + nl txt = str2list(txt) txt = reverse(txt) txt = list2str(txt) see txt fclose(fp)
see nl + "done..." + nl </lang>
- Output:
working... Input file lines: Ring Programming Language #1 Ring Programming Language #2 Ring Programming Language #3 Reversed file lines: Ring Programming Language #3 Ring Programming Language #2 Ring Programming Language #1 done...