use strict; use List::MoreUtils qw/mesh natatime/; use Data::Dumper; my @class = ( 40, 40, 40, 183, 183, 259, 244, 503 ); my @qty = ( 260, 234, 211, 301, 401, 321, 210, 451 ); my %bin = ( 0 => [ 0, 0 ], 40 => [ 0, 0 ], 183 => [ 0, 0 ], 503 => [ 0, 0 ], 442 => [ 0, 0 ], 259 => [ 0, 0 ], 244 => [ 0, 0 ], ); my $it = natatime 2, mesh @class, @qty; while ( my ( $class, $quantity ) = $it->() ) { ( $bin{$class}->[0] )++; $bin{$class}->[1] += $quantity; } print Dumper( \%bin );