my $p = 7; # Number of pigeon holes. my $c = 12; # Number of cards for my $s1 ( 1..$p ) { for my $s2 ( $s1..$p ) { for my $s3 ( $s2..$p ) { ... Need $c-$p loops #### #!/usr/bin/perl -w use strict; use Algorithm::Loops 'NestedLoops'; my( $p, $c ) = @ARGV; my $iter = NestedLoops( [ [ 1..$p ], ( sub { [ $_..$p ] } ) x ($c-$p-1) ], ); my @s; while( @s = $iter->() ) { my @p = (1) x $p; $p[$_-1]++ for @s; print "@p\n"; }