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);

In reply to deleting a hash key can create one? by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.