Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{Draft task}} ;Task:Reverse lines of entire file. <br><br> =={{header|Ring}}== <lang ring> load "stdlib.ring" see "working..." + nl see "Input file lines:" + nl fp = fope...")
 
(→‎{{header|Raku}}: Add a Raku example)
Line 4: Line 4:


<br><br>
<br><br>

=={{header|Raku}}==
Simplest thing that could possibly satisfy the extremely vague task description and completely glossing over all of teh 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>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 14:56, 3 August 2021

Reverse the order of lines in a text file while preserving the contents of each line is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Reverse lines of entire file.



Raku

Simplest thing that could possibly satisfy the extremely vague task description and completely glossing over all of teh 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...