How can I cleanly turn a nested Perl hash into a non-nested one?

Posted by knorv on Stack Overflow See other posts from Stack Overflow or by knorv
Published on 2010-03-22T20:52:16Z Indexed on 2010/03/23 17:33 UTC
Read the original article Hit count: 398

Filed under:
|
|

Assume a nested hash structure %old_hash ..

my %old_hash;
$old_hash{"foo"}{"bar"}{"zonk"} = "hello";

.. which we want to "flatten" (sorry if that's the wrong terminology!) to a non-nested hash using the sub &flatten(...) so that ..

my %h = &flatten(\%old_hash);
die unless($h{"zonk"} eq "hello");

The following definition of &flatten(...) does the trick:

sub flatten {
  my $hashref = shift;
  my %hash;
  my %i = %{$hashref};
  foreach my $ii (keys(%i)) {
    my %j = %{$i{$ii}};
    foreach my $jj (keys(%j)) {
      my %k = %{$j{$jj}};
      foreach my $kk (keys(%k)) {
        my $value = $k{$kk};
        $hash{$kk} = $value;
      }
    }
  }
  return %hash;
}

While the code given works it is not very readable or clean.

My question is two-fold:

  • In what ways does the given code not correspond to modern Perl best practices? Be harsh! :-)
  • How would you clean it up?

© Stack Overflow or respective owner

Related posts about perl

Related posts about clean-up