in reply to reversing files line by line

There are two three four potential approaches to this problem, but as it mostly sounds like homework to me, I'll only outline the general approaches:

The first approach is to observe that reverse(A+B) = reverse(B) + reverse(A), so you can split your long list into many smaller lists, reverse these smaller lists and then join the reversed lists back together. If you want to do this in an recursive approach, you could also observe that reverse(a) == a and treat that as a stopping condition, although that would be quite inefficient, as would be the observation that reverse(a+TAIL) == reverse(TAIL)+a.

The second approach, which works as long as you have enough core memory for storing a list of numbers, would be to read through the whole file, noting the offset of each line (as told via tell), and then to reverse that list of numbers, seek to each offset in the file and to then write out the line from there into the new file. If you are careful with your memory usage, or if you are tricky and store your offsets packed in a string, for example as 4-byte or 8-byte tuples, or maybe even as just the (presumed shorter) list of line-lengths, you can reduce the amount of memory you need to store the information and still reverse the whole file.

A third approach would be to not store any information and just seek a bit backwards from the current location and read data up to the current location, then scan for a line break. If one is found, output the line from there. Seek further backwards.

A fourth approach would be to use File::ReadBackwards.

Replies are listed 'Best First'.
Re^2: reversing files line by line
by naturalsciences (Beadle) on Feb 16, 2010 at 09:47 UTC
    I found this module before I read your post and i might admit it worked really well. If i get some free time on my hands i try to nbble with this problem to solve without modules( just out of curiosity and thanks for other solutions as well, i seem to recall seeing seek used for a thing similar)