in reply to Printing a tree-view of a hash of hashes
Your specification is rather sparse. However a big heads up in your code is that you are using nested loops. That just ain't going to work. Every time you add a level to your hash you will need to add another nested loop and you really don't want to go there.
The common technique is to write a recursive sub to do the work. Consider:
use strict; use warnings; use 5.010; my %categories = ( level1 => { 'level1-1' => {}, 'level1-2' => {'level1-2-1' => {}}, 'level1-3' => {}, }, level2 => {'level2-1' => {}}, ); dumpHash(\%categories); sub dumpHash { my ($subHash, $indent) = @_; $indent //= ''; given (ref $subHash) { when ('') {print "$indent$subHash\n" if defined $subHash} when ('HASH') { for my $key (sort keys %$subHash) { print "$indent$key\n"; dumpHash($subHash->{$key}, "$indent "); } } } }
Prints:
level1 level1-1 level1-2 level1-2-1 level1-3 level2 level2-1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Printing a tree-view of a hash of hashes
by pdxperl (Sexton) on Jun 01, 2010 at 03:24 UTC | |
by ikegami (Patriarch) on Jun 01, 2010 at 03:36 UTC | |
by pdxperl (Sexton) on Jun 01, 2010 at 15:06 UTC | |
by ikegami (Patriarch) on Jun 01, 2010 at 15:39 UTC |