Create a hash from an array of arrays of key|value pairs Starting from an array of sub-arrays, each sub-array is a 2 elements key|value pair. Example

input = [["A", 5], ["B", 2], ["A", 12], ["B", -2], ["C", 7]]


Convert this into a hash that has distinct keys from above, with values summed up, so the above would be:

output = {"A" =&gt; 17, "B" =&gt; 0, "C" =&gt; 7}
Perl

Hopefully this thread won't get tagged code-golf

use strict;
use warnings;

my $a = [["A", 5], ["B", 2], ["A", 12], ["B", -2], ["C", 7]];

my %h = ();

map { $h{shift @$_} += shift @$_ } @$a;

while (my ($k, $v) = each %h)
{
  print "$k = $v $/";
}


Output:

$ perl cg-arraysum.pl 
A = 17 
C = 7 
B = 0