[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Processing file backwards?



At 14.43 -0500 1998.05.22, Nam-June Joe wrote:
>What is the most straightforward to traverse through an ascii file
>backwards, along the lines of:
>
>while ($line = <FILE>)
>{
>	...
>}

Well, if it is a large file, you are kinda in trouble; this is subject to
available memory in MacPerl.  Otherwise, if it is small enough to fit into
memory, you can do:

  #!perl -w
  use strict;

  open FILE, 'Shuck Manual' or die $!;
  my @file = reverse <FILE>;
  foreach my $line (@file) {
    print $line;
  }

If the file is too big, try something like this:

  #!perl -w
  # unfinished code
  use strict;

  open FILE, 'Shuck Manual' or die $!;
  my @tmpfiles;
  while (!eof(FILE)) {
    # open different $tempfile here each time through
    push @tmpfiles, $tempfile;  # path
    my @array;
    while(<FILE>) {push @array, $_}
    print TEMPFILE reverse @array;
  }

  # now open the files in reverse order, doing a while on each

Hope this helps,

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
MacPerl: Power and Ease (ISBN 1881957322), http://www.ptf.com/macperl/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch