For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Printing one txt file to a new txt file backwards









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Printing one txt file to a new txt file backwards
apax999@gmail.com

2006-11-26, 9:56 pm

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;


Any help would be appreciated!

nobull67@gmail.com

2006-11-26, 9:56 pm


apax999@gmail.com 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.

Uri Guttman

2006-11-27, 3:57 am

>>>>> "nc" == nobull67@gmail com <nobull67@gmail.com> writes:

nc> apax999@gmail.com wrote:[color=darkred]

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@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com