Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Deleting hash entry

by Aragorn (Curate)
on Dec 16, 2003 at 15:22 UTC ( #315069=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Deleting hash entry
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-08-23 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls