my @flat_data; for my $file (keys %hoh){ for my $letter (keys %{$hoh{$file}}){ my $weight = $hoh{$file}{$letter}{weight}; my $intensity = $hoh{$file}{$letter}{intensity}; push @flat_data, [ $weight, $intensity, $file ]; } } printf "weight: %d intensity %d Filename: %s\n", @$_ for sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] } @flat_data;