title: perl how to delete the top level/primary hash key including its values for a hash of hash
body: <p>I have a hash of hash value I am getting from a subroutine. Pasting the code chunk below.</p>
  
  <pre><code>sub check_interfaces()
  my @interfaces = IO::Interface::Simple-&gt;interfaces;
  my ($ip, $subnet, $dscp) = @_;
  my %check_config = (
      "ip" =&gt; $ip,
      "subnet" =&gt; $subnet,
      "dscp" =&gt; $dscp
  );
  
  foreach my $if (@interfaces) {
      if (not defined $if-&gt;address) {
          next;
      }
      %check_configs = ( config =&gt; \%check_config );
      my %interface = ($if, $if-&gt;address);
      if ($check_configs{config}{ip} &amp;&amp; $check_configs{config}{ip} =~ /$interface{$if}/) {
          print "$check_configs{config}{ip} already configured\n\n";
          delete $check_configs{config}{ip};
          delete $check_configs{config}{subnet};
          delete $check_configs{config}{dscp};
          delete $check_configs{config};
      }
  }
  print Dumper %check_configs;
  </code></pre>
  
  <p>This is giving me output as</p>
  
  <pre><code>root@server:/l3dsr# perl test.pl start
  10.75.21.23 already configured
  
  $VAR1 = 'config';
  $VAR2 = {};
  $VAR1 = 'config';
  $VAR2 = {
        'dscp' =&gt; '2',
        'ip' =&gt; '10.75.130.24',
        'subnet' =&gt; '255.255.255.255'
      };
  </code></pre>
  
  <p>I was trying to delete the primary hash key, but it was not getting deleted. If I am not deleting, the output would be</p>
  
  <pre><code>10.75.21.23 already configured
  
  $VAR1 = 'config';
  $VAR2 = {
        'dscp' =&gt; '2',
        'ip' =&gt; '10.75.21.23',
        'subnet' =&gt; '255.255.255.255'
      };
  $VAR1 = 'config';
  $VAR2 = {
        'dscp' =&gt; '2',
        'ip' =&gt; '10.75.130.24',
        'subnet' =&gt; '255.255.255.255'
      };
  </code></pre>
  
  <p>What I am trying to do is to get rid of this chunk of hash of hash</p>
  
  <pre><code>$VAR1 = 'config';
  $VAR2 = {
        'dscp' =&gt; '2',
        'ip' =&gt; '10.75.21.23',
        'subnet' =&gt; '255.255.255.255'
      };
  </code></pre>
  
  <p>by doing </p>
  
  <pre><code>            delete $check_configs{config}{ip};
              delete $check_configs{config}{subnet};
              delete $check_configs{config}{dscp};
              delete $check_configs{config};
  </code></pre>
  
  <p>But it is not working as expected. What am I doing wrong here?</p>
  
  <p>Added Dumper just after the delete, but there is nothing being printed. However, I am printing the keys, and you can see the duplicate occurrence of the key here.</p>
  
  <pre><code>    if ($check_configs{config}{ip} &amp;&amp; $check_configs{config}{ip} =~ /$interface{$if}/) {
          print "$check_configs{config}{ip} already configured\n\n";
          delete $check_configs{config}{ip};
          delete $check_configs{config}{subnet};
          delete $check_configs{config}{dscp};
          delete $check_configs{config};
          print Dumper %check_configs;
      }
  }
  print keys %check_configs;
  </code></pre>
  
  <p>Output::</p>
  
  <pre><code>root@server:/l3dsr# perl test.pl start
  10.75.21.23 already configured
  
  configconfigroot@server:/l3dsr# 
  </code></pre>
  
  <p>Borodin: 
      With </p>
  
  <pre><code>$Data::Dumper::Useqq = 1;
  print Dumper [ keys %check_configs ];
  </code></pre>
  
  <p>Output::</p>
  
  <pre><code>root@cdn-fe13:/l3dsr# perl test.pl start
  10.75.21.23 already configured
  
  $VAR1 = [
        "config"
      ];
  $VAR1 = {
        "config" =&gt; {}
      };
  $VAR1 = [
        "config"
      ];
  $VAR1 = {
        "config" =&gt; {
                      "dscp" =&gt; 2,
                      "ip" =&gt; "10.75.130.24",
                      "subnet" =&gt; "255.255.255.255"
                    }
      };
  </code></pre>
  
  <p>Update::</p>
  
  <p>If I add a delete after the delete $check_configs like</p>
  
  <pre><code>            delete $check_configs{config}{subnet};
              delete $check_configs{config}{dscp};
              delete $check_configs{config};
          }
      }
      if (!keys $check_configs{config}) {
          delete $check_configs{config};
      }
      print  keys %check_configs ;
  </code></pre>
  
  <p>it deletes the empty config keys. However, if I add it above, as </p>
  
  <pre><code>    if (!keys $check_configs{config}) {
          delete $check_configs{config};
      }
      if ($check_configs{config}{ip} &amp;&amp; $check_configs{config}{ip} =~ /$interface{$if}/) {....
  </code></pre>
  
  <p>it is not working. I am not sure where it is getting the empty keys from. </p>