zentara has asked for the wisdom of the Perl Monks concerning the following question:

I was working on a project and ran into this "unexpected behavior", and was wondering if some monks would discuss the finer points of adding and deleting hash keys. As a simplified example,in the following script, if I delete $info{'level_b'}, then try to delete $info{'level_b'}{2}{'name'}, $info{'level_b'}{2} gets created by the second "delete". It dosn't seem right to me. Does this mean you have to delete the most deeply nested keys first, and work your way out? Is there some way to deal with this behavior. I would have expected the second delete to just fail. TIA
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %info = ( level_a => { 1 => { name => "a", loc => "b", }, }, level_b => { 2 => { name => "c", loc => "d", }, }, level_c => { 3 => { name => "e", loc => "f", } } ); delete $info{'level_b'} or warn $!; #the following delete creates $info{'level_b'}{2} ?? delete $info{'level_b'}{2}{'name'} or warn $!; #delete $info{'level_b'} or warn $!; #works fine print Dumper(%info);

Replies are listed 'Best First'.
Re: deleting a hash key can create one?
by Roger (Parson) on Feb 04, 2004 at 01:37 UTC
    This is the expected autovivification behaviour in Perl that you should be aware of. That's why you should check the existance of a key and its ancestors' keys before deleting it.

    exists $info{'level_b'} and exists $info{'level_b'}{2} and exists $info{'level_b'}{2}{'name'} and # this line is not necessa +ry delete $info{'level_b'}{2}{'name'} or warn $!;

    I did a super-search and found the following node ... Autovivification with hash of hashes ... that addresses this problem.

    PS: That print Dumper(%info); could be better written as print Dumper(\%info); to give a cleaner presentation of the data structure.

Re: deleting a hash key can create one?
by jdtoronto (Prior) on Feb 04, 2004 at 05:26 UTC
    Autovivification (or autovivication in some peoples lexicon) is a pretty well known feature of Perl. But we all walk slap bang into it at some time!

    In the very same thread that Roger referred you to their is a nifty little subroutine ( Re: Autovivification with hash of hashes ) that you can use to test for existence without autovivifying the shallower levels of the hash. You will also find here a discussion of the subject and some more code that is related to the problem.

    The subject is covered in detail in Programming Perl 3rd edition on page 254.

    jdtoronto

Re: deleting a hash key can create one?
by zentara (Cardinal) on Feb 04, 2004 at 15:44 UTC
    Thanks, now Auto-vivication is now a permanent part of my vocabulary. It's funny how a word dosn't mean a thing to you until you have to use it to solve a problem.

    It makes sense now..."auto-vivify"...."auto-bring-into-existence". I was searching for "hash delete key".

    Also extra thanks Roger for pointing out the "pretty-print" behavior of Dumper(\%info)...a useful "nugget of knowledge".