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.


In reply to Re: Found a word from a set of letters by ikegami
in thread Found a word from a set of letters by vnpenguin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.