eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec perl -w -S $0 $argv:q' if 0; # The above invocation finds perl in the path, wherever it may be # find words in dictionary containing only letters given # usage: $0 given_letters word_list_file_name[s] use strict; use warnings; our $min_word_length = 3; our $given = shift; our $given_length = length( $given ); ####################################### # read in dictionary, removing invalid words, and creating warn "Reading in dictionary, creating dictionary anagrams\n"; our %dictionary; while (<>) { chomp; next unless /^[a-z]{$min_word_length,}$/; # word filter my $sorted_anagram = join '', sort split '', $_; push @{$dictionary{$sorted_anagram}}, $_; } my $anagram_keys = scalar keys %dictionary; our $dictionary_anagram_string = join "\t", sort keys %dictionary; warn "\t$anagram_keys anagram keys in dictionary (after filtering)\n"; ###############################################3 warn "Creating given regex\n"; # convert given to regex strings of the form "a{0,3}b{0,2}..." my @given = split '', $given; my %given_counts; for my $g (@given) { $given_counts{$g}++; } my $given_regex = ''; for my $g (sort keys %given_counts) { $given_regex .= sprintf "%s{0,%d}", $g,$given_counts{$g}; } #warn "\t\$given_regex =~ m/\\b$given_regex\\b/g\n"; ############################################### # find all matching anagram keys warn "Matching anagram keys\n"; my @key_match_results = grep {defined($_) and length($_)} $dictionary_anagram_string =~ m/\b$given_regex\b/g; warn "\t", scalar @key_match_results, " matching anagram keys\n"; # lookup words from keys warn "Looking up matching words\n"; my @words_matched; for my $k (@key_match_results) { push @words_matched, @{$dictionary{$k}}; } print +join(' ', sort @words_matched), "\n"; warn "\t", scalar @words_matched, " words matched\n"; exit;