in reply to Found a word from a set of letters

Sort the letters, sort the letters of the words of a dictionary file, and match them up.

sub canonize { return join '', sort split //, lc $_[0]; } my %dict; { open(my $fh, '<', $dict_file) or die("Unable to open dictionary file \"$dict_file\": $!\n"); while (<$fh>) { chomp; push @{ $dict{canonize($_)} }, $_; } } my $jumble = 'OLHOSC'; $jumble = canonize($jumble); if (exists($dict{$jumble})) { print("$_\n") for @{ $dict{$jumble} }; } else { print("No matches found.\n"); }

You might want to look at using a Trie for a more memory efficient structure than a hash for this purpose.

If you only want one match, and you're only working on one word, you can just process the dictionary file until you find a match.

sub canonize { return join '', sort split //, lc $_[0]; } my $jumble = 'OLHOSC'; open(my $fh, '<', $dict_file) or die("Unable to open dictionary file \"$dict_file\": $!\n"); my $found = 0; $jumble = canonize($jumble); while (<$fh>) { chomp; if (canonize($_) eq $jumble) { print("$_\n"); $found = 1; last; } } if (!$found) { print("No matches found.\n"); }

Update: The others inlined canonize. That's probably a good idea since function calls are expensive and you'll be canonizing a lot of words.