not the prettiest code i've ever written, but it seems to do the trick.
#!perl -wl use strict; $_ = shift @ARGV || "CLEANSE"; our (%kw, %words); my ($match, @matches); my $future = {"" => ""}; sub sortword { return join("", sort pop=~/./g) } sub dissect { my %h; map $h{$_}++, split(//,pop); return \%h } sub scrub { # make sure a word isn't longer than our # kw or has chars kw doesn't my $w = shift @_; return 0 if length($w) > $kw{"len"}; my $d = dissect($w); map { return 0 if !defined($kw{"d"}->{$_}) || $kw{"d"}->{$_} < $d->{$_} } keys %$d; return 1; } sub process { # run one iteration through %words my ($cand, @match, %future) = @_; for my $w (keys %words) { for my $c (keys %$cand) { my $len = length($cand->{$c}) + $words{$w}; next if $len > $kw{"len"}; my $nk = "$c:$w"; $future{$nk} = sortword($cand->{$c} . $w); if ($len == $kw{"len"}) { push @match, $nk if $future{$nk} eq $kw{"kw"}; delete $future{$nk}; } } } return (\@match, \%future); } %kw = ( "word" => $_ ,"len" => length($_) ,"kw" => sortword($_) ,"d" => dissect($_) ); map { chomp; $words{$_} = length() if scrub($_); } <DATA>; while (keys %$future) { ($match, $future) = process($future); push @matches, @$match; } print "anagrams for " . $kw{"word"} . ":"; map print, @matches; __DATA__ SEE SCALENE CLAN EEL BOB PIZZA SCAN MONKS PERL

perl -e'$_="nwdd\x7F^n\x7Flm{{llql0}qs\x14";s/./chr(ord$&^30)/ge;print'


In reply to Re: OT: How to find anagrams? by pizza_milkshake
in thread OT: How to find anagrams? by Cody Pendant

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.