in reply to Sort command equivalent in perl
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort command equivalent in perl
by MidLifeXis (Monsignor) on Dec 16, 2011 at 16:56 UTC | |
by BrowserUk (Patriarch) on Dec 16, 2011 at 17:21 UTC | |
by davido (Cardinal) on Dec 16, 2011 at 17:25 UTC | |
|
Re^2: Sort command equivalent in perl
by BrowserUk (Patriarch) on Dec 16, 2011 at 16:55 UTC |