in reply to Sort lines in a file

This is well known, but DON'T use $&, as it will grind your program to a complete halt.

Next, do the sorting while you're processing the input:

open(my $IN,'<','in.txt') || die "cannot open in.txt - $!\n"; my @output; while(<$IN>) { next if m/^\n/; push (@output, $_); @output = sort @output; }

Software speaks in tongues of man.
Stop saying 'script'. Stop saying 'line-noise'.
We have nothing to lose but our metaphores.

Replies are listed 'Best First'.
Re^2: Sort lines in a file
by toolic (Bishop) on Feb 14, 2008 at 15:47 UTC
    Next, do the sorting while you're processing the input:
    Isn't it faster to do the sorting outside of the loop? What am I missing?
    while(<$IN>) { next if m/^\n/; push (@output, $_); } @output = sort @output;
Re^2: Sort lines in a file
by MidLifeXis (Monsignor) on Feb 14, 2008 at 16:37 UTC

    I don't know that you can say that in general. Some sort algorithms have worst case timings when sorting a pre-sorted (or nearly pre-sorted) list.

    That being said, however, Perl may take that into account.

    I do not, however, without benchmark data, buy that running sort() inside of a loop is faster than running sort outside of the loop.

    Does anyone have a benchmark framework that can test inside and outside sorts with input data that is sorted, reverse-sorted, nearly sorted, and random?

    --MidLifeXis

      But these things will dump the array and occupies more memory. This should not take memory because it should handle a large files.