in reply to recursive hash layer removal question

My inital thoughts: (might update it later)

Change your foreach loop into this:

if (ref $refToHash->{$a} eq 'HASH') { foreach $b (keys %{$refToHash->{$a}}) { $refToHash->{$b} = $refToHash->{$a}->{$b}; } } delete($refToHash->{$a});

The problems with your current code:

Note, if you remove the last layer with this code then the previous layer will point to empty hash reference... is that what you want? (add an else statement to the 'if (ref ..)' if it isn't

Replies are listed 'Best First'.
Re^2: recursive hash layer removal question
by wertert (Sexton) on May 10, 2005 at 17:38 UTC
    Thanks for the fast reply. I'm sorry I normally check for hash reference. Can't understand why I didn't here i must have recursion brain ache. Example is the printTree function removed from the original post.
    sub printTree { my($a)=shift; my(@l); foreach $b (sort keys %{$a}) { if(ref($a->{$b}) eq 'HASH') { $l[$depth]=$b; print "@l\n"; $depth++; printTree($a->{$b}); $depth--; } else { $l[$depth]=$b; print "@l\n"; } } }