[英]Removing a hash element if its value is “undef”
I have seen delete
that deletes a hash element.我见过delete
哈希元素的删除。 But what if some of the elements dates have undef
for a value?但是如果某些元素日期的值具有undef
呢?
I would like to delete all hash elements whose value is undef
.我想删除所有值为undef
哈希元素。
How would I do that in a single line, or the simplest way.我将如何在一行或最简单的方式中做到这一点。
I suggest that you delete a hash slice, using grep
to determine which keys correspond to an undefined value我建议你删除一个hash slice,使用grep
来确定哪些键对应一个未定义的值
use strict;
use warnings 'all';
my %h = (
a => 1,
b => undef,
c => 1,
d => undef,
);
delete @h{ grep { not defined $h{$_} } keys %h };
use Data::Dump;
dd \%h;
{ a => 1, c => 1 }
foreach (keys %hash) {
delete $hash->{$_} unless defined $hash->{$_};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.