I'm looking to custom sort an array gathered from a Net::FTP listing of a directory of logs. Logs are entered by thisisalog-#, where # is 0,1,2,3. This is fine when sorting by the ->ls function of Net::FTP but when it goes to 10,11, the list will start at 1, then next will be 10, not 2. How can we custom sort the array so instead of:
thisisalog-1
thisisalog-10
thisisalog-11
thisisalog-2
thisisalog-3
etc...
it goes like this
thisisalog-1
thisisalog-2
etc...
thisisalog-10
This is my current snippit of code:
my $f = Net::FTP->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";
my @files = $f->ls;
foreach (@files) {
my $file = $_;
print "My file is $file \n";
}
Update - Log format really looks more like this with numbers unfortunantely within as well, just after the dash is it different:
abcd1_abc_123456.abc1a_A.201307290800-0900-0.gz
abcd1_abc_123456.abc1a_A.201307290800-0900-1.gz
abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz
abcd1_abc_123456.abc1a_A.201307290800-0900-11.gz
abcd1_abc_123456.abc1a_A.201307290800-0900-2.gz
abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz
Update: Laurent_R has used a transform method that takes all the constraints into account and makes this work:
my @files = map { $_->[0] }
sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] }
map { [ $_, /\.(\d{12}\-\d{4})/, /.+?-(\d+)\..+$/ ] } <DAT
+A>;
print $_,$/ for @files;
</p>
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.