in reply to Re: Reversing A File
in thread Reversing A File
Your reverse puts all 1 million lines in memory.
That's on top of the 2 million indexes you place in memory when only 1 million are needed.
Fix:
use Tie::File; tie(my @data, 'Tie::File', $fname_in) or die("Unable to open \"$fname_in\": $!\n"); open(my $fh_out, '>', $fname_out) or die("Unable to create \"$fname_out\": $!\n"); for (my $i=@data; $i--; ) { print $fh_out $data[$i]; } untie @data;
Although I recommend File::ReadBackwards.
use File::ReadBackwards qw( ); my $fh_in = File::ReadBackwards->new($fname_in) or die("Unable to open \"$fname_in\": $!\n"); open(my $fh_out, '>', $fname_out) or die("Unable to create \"$fname_out\": $!\n"); while (defined(my $line = $fh_in->readline())) { print $fh_out $line; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Reversing A File
by camelcom (Sexton) on Dec 12, 2007 at 13:40 UTC | |
by marto (Cardinal) on Dec 12, 2007 at 13:50 UTC | |
by runrig (Abbot) on Dec 12, 2007 at 15:42 UTC |