#!/usr/lib/perl use strict; use warnings; my $kPoolSize = 2; my %hash = ( 3 => [ 1 .. 2 ], 4 => [ 2 .. 2 ], 6 => [ 4 .. 5 ], 7 => [ 2 .. 6 ], 8 => [ 1 .. 3 ], 11 => [ 5 .. 10 ], ); my @indexes = sort {$a <=> $b} keys %hash; my @pool = splice @indexes, 0, $kPoolSize; while (@pool == $kPoolSize) { my $poolSum; for my $poolEntry (@pool) { $poolSum += $_ for @{$hash{$poolEntry}}; } print "Pool @pool sum: $poolSum\n"; shift @pool; push @pool, shift @indexes if @indexes; }