我目前的哈希:
%hash = (
"foo",
[
"apple",
"orange",
"apple",
"apple"
],
"bob",
[
"apple",
"orange",
],
);
如何获得此输出?
%hash2 = (
apple => 4,
orange => 2,
);
答案 0 :(得分:3)
my %counts;
for (values(%hash)) { # Iterate over the values of the hash, the references to the arrays.
for (@$_) { # Iterate over the values of the referenced array.
++$counts{$_};
}
}
或
my %counts;
++$counts{$_} for map @$_, values %hash;