How about this?
use strict; use warnings; use File::Copy; use Tie::File; copy( $ARGV[0], $ARGV[1] ) or die $!; my @file; tie @file, 'Tie::File', $ARGV[1] or die $!; @file = sort @file; untie @file;
Tie::File has low memory requirements: The file's lines are treated as a disk-based array. File::Copy concisely copies the source file to a target filename. Then we tie the target file and sort it.
I can't vouch for it being particularly fast; I suspect the OS version of sort is better optimized for disk-based sorting.
Update: Ooh I hate when first instinct is misguided. Well, second instinct really, first instinct is to use the tools that are already written and proven (ie, the Linux sort utility). This muse fails as MidLife pointed out: The sort itself isn't done 'inline', and thus the memory usage will soar. And of course not even Evel Knievel could jump the valley separating the speed of the established tool versus this hack. :)
Dave
In reply to Re: Sort command equivalent in perl
by davido
in thread Sort command equivalent in perl
by sandeepau
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |