Very nice solution. I was also thinking of a succession of ternary operators to handle the undef or "null" special cases, but did not want in my earlier post to decide on those cases without the OP specifying the required behavior for those cases (which is why I suggested an example sorting on another field). Having said that, if the idea is just to sort the (final) sub-arrays (as I understood the OP's requirement), then the two calls to the
map function seem unnecessary. We could have just this (using your test data):
use strict;
use warnings;
my %data = (
A => [ 0, 1, 2, 3, 99, 5, 6 ],
B => [ 0, 1, 2, 3, 9.9999, 5, 6 ],
C => [ 0, 1, 2, 3, "", 5, 6 ],
D => [ 0, 1, 2, 3, undef, 5, 6 ],
E => [ 0, 1, 2, 3, 11, 5, 6 ],
F => [ 0, 1, 2, 3, 123.0, 5, 6 ],
G => [ 0, 1, 2, 3, '', 5, 6 ],
H => [ 0, 1, 2, 3, undef, 5, 6 ],
I => [ 0, 1, 2, 3, -11, 5, 6 ],
J => [ 0, 1, 2, 3, -1.1, 5, 6 ],
);
my @rows = sort {
(! defined $a->[4]) ? -1 :
(! defined $b->[4]) ? 1 :
(! length $a->[4]) ? -1 :
(! length $b->[4]) ? 1 :
($a->[4] <=> $b->[4])
} values %data;
But here again, we don't really know what the OP exactly wants to get.
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.