仅当键没有值时如何删除哈希键?
我构建了一个循环,查找特定虚拟机的所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
计算第一轮删除后剩余的密钥。如果计数为零,则在更高级别删除。
Count the remaining keys after your first round of deletion. If the count is zero, delete at the higher level.