#!/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 )}; }