in reply to Sorting by column
with the main principle being to let Perl use its highly optimised default sorting algorithm. When tweaking how the sort function works, it can get very slow and inefficient pretty easily. With the GRT sort you call the sort function either as plain old sort or reverse sortmap { unpack or substr }sort map{ pack [template, $var1, $var2, etc, $string_or_reference] }
From what I've learnt so far, the pack is essentially prepending a header on the front of each line of text, which the sort function only ever reads upto, before deciding whether two lines are le, ge, or eq.#! usr/bin/perl -w use strict; use warnings; my @original_array = <DATA>; foreach (@original_array){ print "$_"; } my @sorted_array = map{ substr($_,68); #unpack('x68 A*', $_) #achieves same thing but apparently l +ess efficient... } sort map { my ($perm, $path, $filename) = split /\s+/, $_; pack 'N A32 A32 A*', $perm, $path, $filename, $_; } (@original_array); print "\n\n"; foreach (@sorted_array){ print "$_"; } __DATA__ 2755 home 444 home/backup appletest.txt 444 home/backup dhl.txt 444 home/support appletest.bat 2755 bin 755 bin/backup env.txt 755 bin/support arc.bat 755 bin/backup aus.txt 2755 etc 644 etc/backup appletest.txt 644 etc/support arc.bat 644 etc/support dhl.bat 644 etc/support env.bat
|
|---|