Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要在一行中对哈希的不同键求和,而不是使用 foreach。
如果我有一个哈希:
%a = ( a => 4, b => 3, c => 7, d => 2, e => 4 );
例如:$a{ade} 输出:10
这可能是可能的,或者我需要一个foreach?
谢谢
可以使用哈希切片表示法
use List::Utils 'sum'; $the_sum = sum( @a{"a","d","e"} );
my $sum = 0; $sum += $_ for @a{qw( a d e )}; print $sum, "\n";