use mapcar; sub nCr { my( $r, @n )= @_; die "Usage: nCr($r,@n)" if @n < $r; my @pick= ( (0)x(@n-$r), (1)x$r ); my @c; do { push @c, [mapcar{$_[0]?$_[1]:()}\@pick,\@n]; } while( nextpermute( @pick ) ); return \@c; } for( @{nCr(@ARGV)} ) { print "[ ",join(", ",@$_)," ]\n"; } __END__ > nCr 2 a b c [ b, c ] [ a, c ] [ a, b ]