my %hash; push @{$hash{$_}}, $_ for @elements; foreach (keys %hash) { # gets passed an array reference, deref if you so desire do_something_with($hash{$_}) if scalar @{$hash{$_}} > 0; } #### my @data; foreach (keys %hash) { push @data, do_something_with($hash{$_}) if scalar @{$hash{$_}} > 0; } #### my @elems = qw(foo bar baz quux ichi ni san shi go roku hachi); my $set = 3; while(scalar @elems) { do_something_with(splice(@elems, 0, $set)); }