Assuming that I have guessed correctly at the structure of your missing example data, this solves the problem.
#!/usr/bin/perl use strict; use warnings; my %institute_department_roll_name_mark_HoHoHoH = ( CIA => { HUMINT => { rollno1 => { Bob => 65, Adam => 87, }, rollno2 => { Dennis => 37, Arthur => 41, }, }, SIGINT => { rollno1 => { Harry => 87, Albus => 100, }, rollno2 => { M => 70, Q => 98, James => 75, }, }, }, NSA => { Encryption => { rollno1 => { Abel => 13, Baker => 28, Charlie => 57, }, rollno2 => { Roger => 2, Fox => 3, Dog => 2, }, }, Decryption => { rollno1 => { Marin => 62, Chong => 66, }, rollno2 => { Dave => 0, }, }, }, ); # Create a flattened data structure. my $h1 = \%institute_department_roll_name_mark_HoHoHoH; my @institute_department_roll_name_mark_AoH; while ( my ( $institute, $h2 ) = each %{$h1} ) { while ( my ( $department, $h3 ) = each %{$h2} ) { while ( my ( $roll, $h4 ) = each %{$h3} ) { while ( my ( $name, $mark ) = each %{$h4} ) { push @institute_department_roll_name_mark_AoH, { INST => $institute, DEPT => $department, ROLL => $roll, NAME => $name, MARK => $mark, }; } } } } @institute_department_roll_name_mark_AoH = sort { $b->{MARK} <=> $a->{MARK} or $a->{INST} cmp $b->{INST} or $a->{DEPT} cmp $b->{DEPT} or $a->{ROLL} cmp $b->{ROLL} or $a->{NAME} cmp $b->{NAME} } @institute_department_roll_name_mark_AoH; for my $href (@institute_department_roll_name_mark_AoH) { printf "%7d\t%-7s\t%-15s\t%-7s\t%s\n", @{$href}{qw( MARK INST DEPT ROLL NAME )}; }
Output:
100 CIA SIGINT rollno1 Albus 98 CIA SIGINT rollno2 Q 87 CIA HUMINT rollno1 Adam 87 CIA SIGINT rollno1 Harry 75 CIA SIGINT rollno2 James 70 CIA SIGINT rollno2 M 66 NSA Decryption rollno1 Chong 65 CIA HUMINT rollno1 Bob 62 NSA Decryption rollno1 Marin 57 NSA Encryption rollno1 Charlie 41 CIA HUMINT rollno2 Arthur 37 CIA HUMINT rollno2 Dennis 28 NSA Encryption rollno1 Baker 13 NSA Encryption rollno1 Abel 3 NSA Encryption rollno2 Fox 2 NSA Encryption rollno2 Dog 2 NSA Encryption rollno2 Roger 0 NSA Decryption rollno2 Dave

In reply to Re: sorting in hashes by Util
in thread sorting in hashes 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.