I'm a fan of the GRT sort. I like it because it allows multiple keys to be sorted simultaneously in one action. Whether this is a narrow-minded view I have yet to learn... (which I'm still doing :p)
Full info if your interested:
Advanced Sorting - GRT - Guttman Rosler Transform
Original whitepaper discussing sorting in general and GRT:
A Fresh Look at Efficient Perl Sorting
The basic structure of the grt is:
map {
unpack or substr
}sort map{
pack [template, $var1, $var2, etc, $string_or_reference]
}
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 sort
If you really don't care about how sorting works, take a look at
Sort::Maker. It takes a series of arguments and creates a sorting sub for you based on the criteria you provide.
If you are interested on how a GRT sort could be applied to your problem keep reading. :) (and this is by no means the one and only solution. I'm actually interested to see other monks comments, and if my below solution could be improved further)
#! 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
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.
The substr (and unpack), essentially strip off the header portion and return into the array @sorted_array the original line of text.
Hope this is helpful to you
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.