Hi Rob,

Thank you for your response, sorry it wasn't as clear as I thought it might 
have been.

> I have a script, and I want to feed it a special thing to let it know that
> any character (A-Z or a-z does upper lower case matter?) is valid, but I
> also want to use other characters at the same time. So ./script.pl -s ABC is
> valid but also ./script.pl -s AB<any character>DEF is valid.

Indeed, that is exactly it.  Case should be lowercase as my list of words is 
all in lowercase.

> So all you need to do is simply say in case I see the character ? treat this
> as [a-z] or [a-zA-Z] in your regex and you should be fine. A if ( $character
> eq '?' ) { $character = '[a-zA-Z]'; } should do the trick.

I was thinking of using a period ( . ) to mean any character, seeing as no 
punctuation or numbers are present in my word list, it doesn't matter if Perl 
tries to watch them, though I guess it might be slightly inefficient to do 
that...?

> I guess I am thinking to simple here or simply misunderstanding the problem
> as it seems simple enough. It would work if you could for instance post a
> bit of example code so the list can see what you are up to, sometimes a few
> lines of code can say more then a thousand words ;-)

Here is my code, I've taken out a few irrelevant bits, but this is the main 
guts:
use warnings;
use strict;
use Math::Combinatorics;

#Set up a hash for searching later
my %word_list;

#Read list of valid words into hash
my $WORDFILE='Words';
open(WORDFILE, "$WORDFILE") or die "can't open $WORDFILE: $!";
while (<WORDFILE>) {
        chomp;
        $word_list{$_} = 0;
}
close(WORDFILE);

#Set up letters
my @letters = split(//, $ARGV[0]);

#Used to hold all valid combinations of characters
my @all_combinations;

#Iterate through the letters
#We then calculate all combinations
#of the letters and see if any of them match a real word.  If they
#do, we add them to the @all_combinations array.
my $count = 3;
while($count <= @letters){
        my $combinator1 = Math::Combinatorics->new(
                count => $count,
                data => [@letters],
        );
        
        while(my @combo = $combinator1->next_combination){
                my $combinator2 = Math::Combinatorics->new(
                        count => $count,
                        data => [@combo],
                );
                
                while(my @perm = $combinator2->next_permutation){
                        my $temp_word = join('',@perm);
                        
                        #Here, using a hash looks much cleaner than iterating 
through an array
                        push(@all_combinations, $temp_word) if (exists 
$word_list{$temp_word});
                }
        }
        $count++;
}

#Remove duplicates
my %hash = map { $_ => 1 } @all_combinations;
@all_combinations = keys %hash;

#Print out the array, separating each element with a newline.
print join("\n",@all_combinations),"\n";


Currently, I can call the script as below and get the following output:
$ perl combinations.pl abcde
bade
bed
dace
bad
dab
bead
ace
bac
cad
aced
cade
abc
ade
cab

I want to be able to specify something like:
$ perl combinations.pl ab.de
This would essentially be the same as doing:
cat Words | grep -e "^ab.de$"
Where Words is a file containing all of the valid words.

I'm wondering if I need to convert my hash of valid words into an array, then 
iterate through each entry with something like:
if($word_list[i] =~ m/$temp_word/){
        push(@all_combinations, $temp_word);
}

Ben
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to