Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^4: Printing a tree-view of a hash of hashes

by pdxperl (Sexton)
on Jun 01, 2010 at 15:06 UTC ( [id://842632]=note: print w/replies, xml ) Need Help??


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

Thanks -- I didn't mean that comment negatively, as I appreciate the help. Your example is fine with your data, but with mine it did appear to go into infinite recursion. And I agree, there is something odd with what's in the hash, but what I didn't understand was why was is OK in Data::Dumper? So I did track down the cause,which is boiled down to the testcase below. This is what strict was complaining about as the string ref error. I fixed the issue, however, I'm not sure why it causes the tree to be displayed so differently for my approach versus yours versus data dumper. I commented strict out so you can see how the display differs, I know strict normally shouldn't be commented out. Also, this is deliberately showing an error - I'm just showing the odd output that results from that error.
#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

    Why is use strict; commented out!? As previously mentioned, this is hiding serious errors. Specifically, you are trying to store both "1" and a hash reference at $h{b}{1}. One simple fix is to use undef instead of 1.

    use strict; use warnings; use Data::Dumper; my %h; $h{'B'}{1}=undef; for my $a ('A'..'C'){ for my $b (0..2){ for my $c ('a'..'c') { $h{$a}{$b}{$c}=undef; } } } print "Data Dumper-------------------\n"; print Dumper (\%h); print "Loop----------------\n"; foreach my $top (sort keys %h) { print $top =~ /^\s*$/ ? "<blank>" : $top, "\n"; next if !ref($h{$top}); foreach my $second (sort keys %{$h{$top}}) { print "...", $second =~ /^\s*$/ ? "<blank>" : $second, "\n"; next if !ref($h{$top}{$second}); foreach my $third (sort keys %{$h{$top}{$second}}) { print "......", $third =~ /^\s*$/ ? "<blank>" : $third, "\ +n"; next if !ref($h{$top}{$second}{$third}); } } } print "Print_tree------------------\n"; print_tree(\%h); sub print_tree { my ($tree, $depth) = @_; $depth ||= 0; if ($depth > 5) { warn "max depth reached\n"; return; } my $indent = '...' x $depth; for (sort keys %$tree) { print($indent, /^\s*\z/ ? "<blank>" : $_, "\n"); print_tree($tree->{$_}, $depth+1) if ref($tree->{$_}); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://842632]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found