in reply to Sorting values of multi-dimensional hashes while retaining key values

Here's a more generic way to flatten a hash with an arbitrary number of levels. It can be easily extended to handle embedded array refs, as well:

#!/usr/bin/perl -w use strict; my %hash = ( APP1 => { error => { 6001 => 10, 6002 => 15, 6003 => 25, }, alert => { 1001 => 2, 1005 => 200, }, warning => { '0022' => 10, }, }, APP2 => { error => { 6001 => 4, 6004 => 21, }, alert => { 1002 => 3, 1005 => 165, }, }, ); my $separator = '.'; sub flattenHash { my $hashRef = shift; my $prefixArrayRef = shift || []; my $outputArrayRef = shift || []; while ( my ( $key, $value ) = each(%$hashRef) ) { push @$prefixArrayRef, $key; if ( UNIVERSAL::isa( $value, 'HASH' ) ) { flattenHash( $value, $prefixArrayRef, $outputArrayRef ); } else { push ( @$outputArrayRef, join ( $separator, @$prefixArrayR +ef ), $value ); } pop @$prefixArrayRef; } return wantarray ? @$outputArrayRef : $outputArrayRef; } my %flatHash = flattenHash( \%hash ); my @sorted = sort { $b->[0] <=> $a->[0] } map { [ $flatHash{$_}, $_ ] } keys(%fla +tHash); foreach my $arrayRef (@sorted) { print "$arrayRef->[0] $arrayRef->[1]\n"; }

update: made sure of no value collisions in reverse, quoted octal key.

  • Comment on Re: Sorting values of multi-dimensional hashes while retaining key values
  • Download Code