On Thu, May 05, 2011 at 08:20:13AM -0700, eventual wrote: > Hi, Concerning the game Lotto, how do I generate a list of 6 numbers out of > 12 > numbers, eg > > given the 12 numbers as 1,2,3,4,5,6,7,8,9,10,11,12 > the list of 6 numbers goes like this :- > 1,2,3,4,5,6 > 1,2,3,4,5,7 > 1,2,3,4,5,8 > 1,2,3,4,5,9 > 1,2,3,4,5,10 ......... etc etc.... > Thanks
There appears to be a couple of modules on CPAN which can help you: http://search.cpan.org/perldoc?Math::Combinatorics http://search.cpan.org/perldoc?Algorithm::Combinatorics And here's a little fun which uses the regexp engine to do the work for us: #!/usr/bin/perl use 5.12.0; use re "eval"; my $r = join ".*?", map qr/(?<a$_>\b\d+\b)/, 1..shift; my $s = join " ", 1..shift; my @c; $s =~ /$r(?{push @c, [values %+]})(?!)/; say join ",", sort { $a <=> $b } @$_ for @c; Use it as: $ perl comb 6 12 -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/