in reply to recursive hash layer removal question

Oops - was decrementing twice. Thanks for the quick notice, Roy Johnson.
use strict; use warnings; use Data::Dumper; my $W={ 'A' => { 'L' => { 'D'=>99, 'X'=>99, }, 'C' =>{ 'E'=>99 }, }, 'X' => { 'Q'=>{ 'O'=>99, }, 'T'=>{ 'U'=>99, }, }, }; remove($W, 2); print Dumper($W); sub remove { my ($p, $d) = @_; if ($d-- > 1) { remove($_, ($d)) for values %$p; # Had second decrement here } else { my %newhash; for (values %$p) { @newhash{keys %$_} = values %$_; } $_[0] = \%newhash; } }

Replies are listed 'Best First'.
recursive hash layer removal question
by wertert (Sexton) on May 11, 2005 at 14:01 UTC
    Thanks for all your help. Lots of good stuff here. At the risk of appearing useless I may need some help with code provided by TedPride ( very cool BTW ).
    sub remove { my ($p, $d) = @_; if ($d-- > 1) { remove($_, ($d)) for values %$p; # Had second decrement here } else { my %newhash; for (values %$p) { @newhash{keys %$_} = values %$_; } $_[0] = \%newhash; } }
    I've not seen one of these before. It's an hash but it looks like an array ie '@'
    for (values %$p) { @newhash{keys %$_} = values %$_; } $_[0] = \%newhash;
    What is going on here then ? Checked out my cookbook etc but can't find a reference to this syntax. One other slight complication is the data can actually look like this;
    $W={ 'A' => { 'L' => { 'D'=>99, 'X'=>99, }, 'C' =>{ 'E'=>99 }, }, 'X' => { 'Q'=>{ 'O'=>99, }, 'T'=>{ 'U'=>99, }, }, 'W' => 99, };
    Now when we collapse level 2 ie LCQT I don't want to change the value for 'W'. So only drop values on the target level if they are HASH. So really it's a mix of H,HOH and HOHOH etc. Thanks again for your help. Jeff
      It's a hash slice assignment (in other words, it's not a hash, it is an array). Basically you're saying, here is a list of keys and for each of these keys assign these values (since 'keys' and 'values' return lists). I can't find any particular documentation on it, but it does work :)