Re: Printing one txt file to a new txt file backwards




apax999@xxxxxxxxx wrote:
I need to print an entire text file to a new text file backwards. I
have the following code, but it only prints the first line backwards,
and completely omits all the other lines of the original text file:

open IN, "< $file1";
open OUT, "> $file2";
print OUT reverse split '', <IN>;
close IN;
close OUT;

What is the purpose of the split? Why break a string in to a list of
characters and reverse the list? Why not just reverse the string?

Note that reverse() can do two different things. See perldoc -f
reverse.

If you want to reverse the whole file as a single string then you' ll
need to read the whole file as a single string. See FAQ "How can I
read in an entire file all at once?".

{
local $/;
print OUT scalar reverse do <IN>;
}

If you have larger files then consider using File::ReadBackwards.

.