in reply to Re^3: Printing a tree-view of a hash of hashes
in thread Printing a tree-view of a hash of hashes
#use strict; use Data::Dumper; my %h; $h{'B'}{1}=1; # this is the error which causes the display # issues for my $a ('A'..'C'){ for my $b (0..2){ for my $c ('a'..'c') { $h{$a}{$b}{$c}=1; # error if $h{$a}{$b} exists } } } print "Data Dumper-------------------\n"; print Dumper (\%h); print "Loop----------------\n"; foreach my $top ( sort keys %h){ if($top =~ /^\s*$/){$top="<blank>";} print "$top\n"; foreach my $second ( sort keys %{$h{$top}}){ if($second =~ /^\s*$/){$second="<blank>";} print "...$second\n"; foreach my $third (sort keys %{$h{$top}{$second}}){ if($third =~ /^\s*$/){$third="<blank>";} print "......$third\n"; } } } print "Print_tree------------------\n"; print_tree(\%h); sub print_tree { my ($tree, $depth) = @_; if($depth > 5) { print "max depth reached\n"; return; } $depth ||= 0; my $indent = '...' x $depth; for (sort keys %$tree) { print($indent, /^\s*\z/ ? "<blank>" : $_, "\n"); print_tree($tree->{$_}, $depth+1); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Printing a tree-view of a hash of hashes
by ikegami (Patriarch) on Jun 01, 2010 at 15:39 UTC |