Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://315069]
help
Chatterbox?
[Discipulus]: are we not yet in Summer?
Discipulus yesteday saw flowered poppins outside..
[marto]: This won't take till Summer to complete I hope ;)

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2017-12-18 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (498 votes). Check out past polls.

    Notices?