如何在现有的hash中添加元素,如push-in数组,但使用映射?
如果我这样做:
%existing_hash = map { $_ => 1 } @new_elements;
这将重置%existing_hash。
尝试:
%existing_hash = (%existing_hash, map { $_ => 1 } @new_elements);
我想我会用简单的方法来做:
$existing_hash{$_} = 1 for @new_elements;
但是你也可以使用散列切片:
@existing_hash{@new_elements} = (1) x @new_elements;