仅当键没有值时如何删除哈希键?

发布于 2024-10-11 21:42:44 字数 1346 浏览 3 评论 0原文

我构建了一个循环,查找特定虚拟机的所有 VMDK,然后创建输出的哈希值,然后通过在 VMX 文件中查找参数来测试磁盘是否实际存在。然后,如果磁盘不存在,则会将其从哈希中删除。我遇到的问题是如何删除没有定义磁盘的哈希键。

这是代码块;

    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"}
        }
    } 

当上述语句成立并且删除丢失的磁盘时,这就是我得到的哈希值。

$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' => {}
                  }

正如您所看到的,子哈希“IDE0”为空,因为列出的磁盘不存在于 VMX 文件中。现在我想做的是删除整个“IDE0”哈希,因为其中没有任何内容。但是我只希望它在没有哈希值的情况下删除它,因为根据 IDE 规范,它最多可以有 2 个磁盘。

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 '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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2024-10-18 21:42:44

计算第一轮删除后剩余的密钥。如果计数为零,则在更高级别删除。

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

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"}
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文