To add to davorg's point about needing an array, here's one way you might do it.
#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; my $hash_value = { '75.34.42.17511726663' => { '2005' => ['11.5', '363,842'], '1995' => ['', '200,000'], '2004' => ['9.2', '326,345'], '2001' => ['6.7', '263,645'], '1995' => ['','200,000'], '1997' => ['2.6', '212,118'], '1998' => ['3.7', '219,980'], '2002' => ['7.6', '283,665'], '2006' => ['9.4', '398,148'], '2000' => ['7.7', '247,015'], '1996' => ['3.3', '206,680'], '1999' => ['4.2', '229,281'], '2003' => ['5.4', '298,857'], } }; my $key = q{75.34.42.17511726663}; my @sorted_by_year; for my $year (sort keys %{$hash_value->{$key}}) { push @sorted_by_year, {$year => $hash_value->{$key}{$year}}; } print Dumper \@sorted_by_year;
output:
$VAR1 = [ { '1995' => [ '', '200,000' ] }, { '1996' => [ '3.3', '206,680' ] }, { '1997' => [ '2.6', '212,118' ] }, { '1998' => [ '3.7', '219,980' ] }, { '1999' => [ '4.2', '229,281' ] }, { '2000' => [ '7.7', '247,015' ] }, { '2001' => [ '6.7', '263,645' ] }, { '2002' => [ '7.6', '283,665' ] }, { '2003' => [ '5.4', '298,857' ] }, { '2004' => [ '9.2', '326,345' ] }, { '2005' => [ '11.5', '363,842' ] }, { '2006' => [ '9.4', '398,148' ] } ];

In reply to Re: problem while sorting hash by wfsp
in thread problem while sorting hash by Anonymous Monk

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.