Re: Printing one txt file to a new txt file backwards
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Sun, 26 Nov 2006 23:51:06 -0500
"nc" == nobull67@gmail com <nobull67@xxxxxxxxx> writes:
nc> 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;
nc> What is the purpose of the split? Why break a string in to a list of
nc> characters and reverse the list? Why not just reverse the string?
nc> Note that reverse() can do two different things. See perldoc -f
nc> reverse.
nc> If you want to reverse the whole file as a single string then you' ll
nc> need to read the whole file as a single string. See FAQ "How can I
nc> read in an entire file all at once?".
nc> {
nc> local $/;
nc> print OUT scalar reverse do <IN>;
nc> }
nc> If you have larger files then consider using File::ReadBackwards.
or use another of my modules:
use File::Slurp ;
write_file( $file2, reverse read_file( $file1 ) ) ;
you can do the same thing with File::ReadBackwards as it will slurp if
it is tied and you call <> on it in a list context.
use File::Slurp ;
use File::ReadBackwards ;
tie *FH, File::ReadBackwards, $file or die "can't tie '$file1' $!" ;
write_file( $file2, reverse <FH> ) ;
can you tie a lexical handle as with open these days? tie's syntax
chooses how to tie based on the type of its first arg, not its value.
uri
--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.
- References:
- Printing one txt file to a new txt file backwards
- From: apax999
- Re: Printing one txt file to a new txt file backwards
- From: nobull67@xxxxxxxxx
- Printing one txt file to a new txt file backwards
- Prev by Date: Re: Grep
- Next by Date: Win32::TieRegistry->Connect - How to specify credentials on remote machine?
- Previous by thread: Re: Printing one txt file to a new txt file backwards
- Next by thread: Grep
- Index(es):
Relevant Pages
|