sub combinations { my @list= @_; my @pick= (0) x @list; return sub { my $i= 0; while( 1 < ++$pick[$i] ) { $pick[$i]= 0; return if $#pick < ++$i; } return @list[ grep $pick[$_], 0..$#pick ]; }; } my $next= combinations( 50..59 ); my @comb; while( @comb= $next->() ) { # do work with @comb here } # Note that the empty set is a valid combination but is # the last combination returned which also indicates "no # more combinations left. So the above loop doesn't bother # processing the empty list. If you want to process the # empty set, then use: my @comb; do { # do work with @comb here } while( @comb= $next->() );