Anonymous user
Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions
Reverse the order of lines in a text file while preserving the contents of each line (view source)
Revision as of 19:37, 5 August 2021
, 2 years ago→{{header|REXX}}: added a 2nd version which if faster and uses less memory (storage).
(→{{header|REXX}}: added a 2nd version which if faster and uses less memory (storage).) |
|||
Line 247:
=={{header|REXX}}==
=== version 1 ===
This will work for all REXXes, but it reads all the file's lines/records into memory (storage or buffers).
No assumptions were made concerning line/record termination, as REXX takes care of that.
<lang rexx>/*REXX pgm reads a file, and displays the lines (records) of the file in reverse order. */
parse arg iFID . /*obtain optional argument from the CL.*/
if iFID=='' | iFID=="," then iFID='REVERSEF.TXT' /*Not specified? Then use the default.*/
call lineout
do #=1 while lines(iFID)>0 /*read the file, one record at a time. */
recs= # - 1
recs= j - 1 /*J will be 1 more ('cause of DO loop)*/▼
do k=recs by -1 for recs /*process array (@.k) in reverse order.*/
say @.k /*display a record of the file ──► term*/
end /*k*/
call lineout
{{out|output|text= when using the default input:}}
<pre>
Line 269 ⟶ 271:
"Diplomacy is the art of
</pre>
=== version 2 ===
This will work for all the following REXXes (and perhaps other REXXes as well):
::* Regina REXX
::* R4 REXX
::* ROO REXX
::* CMS REXX
<lang>/*REXX pgm reads a file, and displays the lines (records) of the file in reverse order. */
parse arg iFID . /*obtain optional argument from the CL.*/
if iFID=='' | iFID=="," then iFID='REVERSEF.TXT' /*Not specified? Then use the default.*/
call lineout iFID /*close file, good programming practice*/
options noFast_lines_BIF_default /*an option just for Regina REXX. */
do j=# by -1 for # /*read file (backwards), from bot──►top*/
@.j= linein(iFID, j) /*assign contents of a record to array.*/
say @.j /*display a record of the file ──► term*/
end /*j*/
call lineout iFID /*close file, good programming practice*/</lang>
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|Ring}}==
|