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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.