Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Deleting hash entry

by Aragorn (Curate)
on Dec 16, 2003 at 15:22 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-18 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found