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



"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
.



Relevant Pages

  • Re: Simple way to Reverse an Array
    ... Are you saying you don't understand that you can read it forwards into ... an array and just use the reverse() function to process ... Of course, if you REALLY want to read the file backwards, you can use ...
    (comp.lang.perl.misc)
  • Re: Open text file from the from the end backwards
    ... Append those to a collection and reverse the order just prior to using it. ... store each line into a collection that contains a maximum of 20 items. ... reverse the order and limit the number of items you display. ... do I read lines from a text file backwards. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Printing one txt file to a new txt file backwards
    ... and completely omits all the other lines of the original text file: ... print OUT reverse split '', ... If you want to reverse the whole file as a single string then you' ll ...
    (perl.beginners)
  • Re: Open text file from the from the end backwards
    ... Append those to a collection and reverse the order just prior to using it. ... reverse the order and limit the number of items you display. ... do I read lines from a text file backwards. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Open text file from the from the end backwards
    ... Append those to a collection and reverse the order just prior to using it. ... store each line into a collection that contains a maximum of 20 items. ... reverse the order and limit the number of items you display. ... do I read lines from a text file backwards. ...
    (microsoft.public.dotnet.languages.vb)