我有两个哈希,一个大一个小.所有较小的哈希的键显示在较大的哈希中,但值不同.我想将值从更大的哈希复制到较小的哈希.
例如.:
# I have two hashes like so %big_hash = (A => '1',B => '2',C => '3',D => '4',E => '5'); %small_hash = (A => '0',B => '0',C => '0'); # I want small_hash to get the values of big_hash like this %small_hash = (A => '1',C => '3');
一个明显的答案是循环通过小哈希的键,并复制这样的值
foreach $key (keys %small_hash) { $small_hash{$key} = $big_hash{$key}; }
有更短的方法来做到这一点吗?
解决方法
@small_hash{ keys %small_hash } = @big_hash{ keys %small_hash };