http://www.perlmonks.org?node_id=315069


in reply to Deleting hash entry

First of all, the notation %hash = { ... is wrong. You either assign a hash with %hash = ( ... or a hash reference with $hash = { .... Watch those parentheses and curly braces. See perlreftut and perlref for more information.

As for deleting:

#!/usr/bin/perl use Data::Dumper; $hash = { abc => { 'def' => 1, }, ghi => { jkl => 1, mno => 1, }, }; print Dumper(\$hash); delete($hash->{abc}->{def}); print Dumper(\$hash); __END__ $VAR1 = \{ 'abc' => { 'def' => 1 }, 'ghi' => { 'mno' => 1, 'jkl' => 1 } }; $VAR1 = \{ 'abc' => {}, 'ghi' => { 'mno' => 1, 'jkl' => 1 } };
The second question: I don't know :-). Having an undefined hash value is perfectly valid, so the upper level is not automatically deleted when the lower level becomes undefined. You could add a statement like delete $hash->{$key} if not keys %{$hash->{$key}}. This deletes the key only if the hash it references is empty.

Arjen