G'day shekarkcb,
I got rid of the irrelevent data from the hash. I added more test data to better test the sorting process.
I've assumed that (1) "'NULL'" means a zero-length string; (2) the wanted sort order is undefs first, then zero-length strings, then numerical data in ascending order; (3) the output is an array of arrays for writing to a spreadsheet.
#!/usr/bin/env perl 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 = map { [ $_->[0] => @{$data{$_->[0]}} ] } sort { (! defined $a->[1]) ? -1 : (! defined $b->[1]) ? 1 : (! length $a->[1]) ? -1 : (! length $b->[1]) ? 1 : ($a->[1] <=> $b->[1]) } map { [ $_ => $data{$_}[4] ] } keys %data; use Data::Dump; dd \@rows;
Output:
[ ["H", 0 .. 3, undef, 5, 6], ["D", 0 .. 3, undef, 5, 6], ["C", 0 .. 3, "", 5, 6], ["G", 0 .. 3, "", 5, 6], ["I", 0 .. 3, -11, 5, 6], ["J", 0 .. 3, -1.1, 5, 6], ["B", 0 .. 3, 9.9999, 5, 6], ["E", 0 .. 3, 11, 5, 6], ["A", 0 .. 3, 99, 5, 6], ["F", 0 .. 3, 123, 5, 6], ]
-- Ken
In reply to Re: Need to sort and data structure based on values inside arrayrefs.
by kcott
in thread Need to sort and data structure based on values inside arrayrefs.
by shekarkcb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |