Re: Printing one txt file to a new txt file backwards
- From: "nobull67@xxxxxxxxx" <nobull67@xxxxxxxxx>
- Date: 26 Nov 2006 12:27:42 -0800
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.
.
- Follow-Ups:
- Re: Printing one txt file to a new txt file backwards
- From: Uri Guttman
- Re: Printing one txt file to a new txt file backwards
- References:
- Printing one txt file to a new txt file backwards
- From: apax999
- Printing one txt file to a new txt file backwards
- Prev by Date: Printing one txt file to a new txt file backwards
- Next by Date: Grep
- Previous by thread: Printing one txt file to a new txt file backwards
- Next by thread: Re: Printing one txt file to a new txt file backwards
- Index(es):