use strict; use warnings; use Data::Dump::Streamer; my @class = (40, 40, 40, 183, 183, 259, 244, 503); my @qty = (260, 234, 211, 301, 401, 321, 210, 451); my %bin = map {$_ => [0, 0]} (0, 40, 183, 503, 442, 259, 244); for my $i (0 .. $#class) { my $hkey = $class[$i]; @{$bin{$hkey}} = ($bin{$hkey}[0]+1, $bin{$hkey}[1]+ $qty[$i]); } Dump (\%bin);