in reply to Finding all sets of chars
Are you sure you want combinations? There is a difference between combinations and permutations and you also need to consider replacement and duplicates. I think the following code, adapted from How To: Make An Iterator does what you want.
#!/usr/bin/perl use strict; use warnings; # Change this as desired my $size = 2; my $next_perm = fix_size_perm($size, 'a'..'z', 0..9); while ( my $perm = $next_perm->() ) { print "$perm\n"; } sub fix_size_perm { my ($size, @list) = @_; my @curr = (0) x ($size - 1); push @curr, -1; return sub { if ( (join '', map { $list[ $_ ] } @curr) eq $list[ -1 ] x @cu +rr ) { @curr = (0) x (@curr + 1); } else { my $pos = @curr; while ( --$pos > -1 ) { ++$curr[ $pos ], last if $curr[ $pos ] < $#list; $curr[ $pos ] = 0; } } return undef if @curr > $size; return join '', map { $list[ $_ ] } @curr; }; }
Cheers - L~R
Update: Changed code example to hopefully better fit the problem.
|
---|