我在我的程序中使用哈希哈希.我想在同一行中将两个值相加.我的哈希的例子是:
%data_final = (
2010 => {
Jan => {
group1 => {
Type1 => {
Total1 => 12,
Total2 => 10
},
Type2 => {
Total1 => 17,
Total2 => 14
},
},
},
}
);
Run Code Online (Sandbox Code Playgroud)
我想为group1获得一个很大的Total1和Total2.我正在做以下事情:
$total1_sum = sum( @{$data_final}{2010}{Jan}{group1}}{qw/Type1 Type2}{Total1} );
$total2_sum = sum( @{$data_final}{2010}{Jan}{group1}}{qw/Type1 Type2}{Total2} );
Run Code Online (Sandbox Code Playgroud)
$ total1_sum的输出应为29,$ total2_sum的输出应为24,但我在"} {"附近出现语法错误.
非常感谢你的帮助.
我需要在一行中对哈希的不同键求和,而不是使用foreach.
如果我有哈希:
%a = (
a => 4,
b => 3,
c => 7,
d => 2,
e => 4
);
Run Code Online (Sandbox Code Playgroud)
例如:$ a {ade}输出:10
它可能还是我需要一个foreach?
谢谢