$W={ 'A' => { 'L' => { 'D'=>99, 'X'=>99, }, 'C' =>{ 'E'=>99 }, }, 'X' => { 'Q'=>{ 'O'=>99, }, 'T'=>{ 'U'=>99, }, }, }; #### $X={ 'A' => { 'D'=>99, 'X'=>99, 'E'=>99 }, 'X' => { 'O'=>99, 'U'=>99, }, }; #### printTree($W); # just dumps HOHOH - could use data::dumper - removed remove1($W,2); # ie remove layer 2 ( counting from 1 ) print "--------------------\n"; printTree($W); sub remove1 { my($refToHash)=shift; my($stage)=shift; foreach $a (keys %{$refToHash}) { if($depth==$stage-1) # then we've hit the level before the level to be removed/ { print "removing $a\n"; foreach $b (keys %{$refToHash->{$a}}) { $refToHash->{$b} = $refToHash->{$a}->{$b}; delete($refToHash->{$a}); } } else { $depth++; remove1($refToHash->{$a},$stage); $depth--; } } } #### perl delete_mid2.pl A C E L D X X Q O T U removing C removing L removing Q removing T -------------------- A D E X X O U #### sub deep_copy { my $this = shift; if (not ref $this) { return $this; } elsif(ref $this eq "HASH") { return {map { $_ => deep_copy($this->{$_}) } keys %$this}; } }