开发者

How would I delete hash keys only when the key has no values?

开发者 https://www.devze.com 2023-02-04 05:08 出处:网络
I have built a loop that finds all of the VMDKs for a perticular VM and then create a hash of the output, then it tests whether the disk is actually present by looking for a parameter in the VMX file.

I have built a loop that finds all of the VMDKs for a perticular VM and then create a hash of the output, then it tests whether the disk is actually present by looking for a parameter in the VMX file. Then if the disk is not present it deletes it from the hash. The problem I running into is how to delete a hash key that has no disks defined.

Here is the code block;

    while ($vmx_file =~ m/^(ide(?<PORT>[0-1])\:(?<DISK>[0-1]))\.present\s+=\s+"(?<PRESENT>[^"]+)["]/xmg) {
        $ide_port = "$+{PORT}";
        $ide_disk = "$+{DISK}";
        $present = "$+{PRESENT}";
        if ($present eq 'FALSE') {
            delete $virtual_disks{$vm}{"IDE$ide_port"}{"Disk$ide_disk"}
        }
    } 

This is what I am getting as a hash when the above statement is true and it deletes the missing disks.

$VAR1 = {
      'Test01' => {
                    'SCSI0' => {
                                 'Disk0' => '/vmfs/volumes/4c8fd27b-5876fc36-80f4-0015179fd63c/Test01/Test01.vmdk',
                                 'Type' => 'lsilogic',
                                 'Disk1' => '/vmfs/volumes/4c8fd27b-5876fc36-80f4-0015179fd63c/Test01/Test01_1.vmdk'
                               },
                    'IDE1' => {
                                'Disk0' => '/vmfs/volumes/4c8fd27b-5876fc36-80f4-0015179fd63c/ubuntu-10.10-desktop-i386.iso'
                              },
                    'IDE0' => {}
                  }

As you can see the sub hash 'IDE0" is empty, because the disk that was listed was not present in the VMX file. Now what I would like to do is remove the entire开发者_如何学C 'IDE0' hash because there is nothing in it. But I only want it to delete it if it hash nothing. becasue it can have up to 2 disks in it as per IDE specs. Follow me?


Count the remaining keys after your first round of deletion. If the count is zero, delete at the higher level.

 if (scalar keys %{ $virtual_disk{$vm}{"IDE$ide_port"} } == 0) {
      delete $virtual_disks{$vm}{"IDE$ide_port"}
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号