in reply to Re: Need to sort and data structure based on values inside arrayrefs.
in thread Need to sort and data structure based on values inside arrayrefs.

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.
  • Comment on Re^2: Need to sort and data structure based on values inside arrayrefs.
  • Download Code

Replies are listed 'Best First'.
Re^3: Need to sort and data structure based on values inside arrayrefs.
by kcott (Archbishop) on Apr 03, 2014 at 13:58 UTC

    What you have there is exactly (or extremely close to) what I originally coded; however, after considering the OP's "writing these values to excel", I quickly changed that to a Schwartzian Transform.

    You're quite correct about not knowing what the OP really wanted. With any of the suggested solutions, questions regarding how an undef is written to a spreadsheet remain outstanding (and this would almost certainly require further coding).

    -- Ken