在Perl中将数组另外映射到现有哈希
如何使用映射在现有哈希(如推入式数组)中添加元素 如果我这样做:在Perl中将数组另外映射到现有哈希,perl,hash,hashmap,Perl,Hash,Hashmap,如何使用映射在现有哈希(如推入式数组)中添加元素 如果我这样做: %existing_hash = map { $_ => 1 } @new_elements; 这将重置%existing\u散列。请尝试: %existing_hash = (%existing_hash, map { $_ => 1 } @new_elements); 尝试: 我想我应该用简单的方法: $existing_hash{$_} = 1 for @new_elements; 但您也可以使用散列切片:
%existing_hash = map { $_ => 1 } @new_elements;
这将重置%existing\u散列。请尝试:
%existing_hash = (%existing_hash, map { $_ => 1 } @new_elements);
尝试:
我想我应该用简单的方法:
$existing_hash{$_} = 1 for @new_elements;
但您也可以使用散列切片:
@existing_hash{@new_elements} = (1) x @new_elements;
我想我应该用简单的方法:
$existing_hash{$_} = 1 for @new_elements;
但您也可以使用散列切片:
@existing_hash{@new_elements} = (1) x @new_elements;
您的第一个解决方案也很好,看起来很简单,谢谢您的好主意。您的第一个解决方案也很好,看起来很简单,谢谢您的好主意。