A while ago I wrote a little program that does a variety of things with anagrams. What follows is a stripped down version that simply prints all the anagrams of the indicated word.

Note: if desired, size restrictions can be applied to the word in three ways:
1) a simple integer - will return anagrams of that size
2) an integer range (3-5) - will return anagrams whose length is within the range (in this case 3,4,5)
3) a mathematical expression (such as >4, <5, !=6) - the anagrams returned will be of size >4, size <5, and size !=6.
#!/usr/local/bin/perl use strict vars; my ($w, $size, $entry, $eval, $cont, $line, $word); my (@combs, @word); my (%anagrams, %bysize, %combs); #read dictionary file my $file = shift; open(FILE, "$file") or die "cannot open file $file: $!\n"; print "reading dictionary...\n"; push( @{ $anagrams{join("", sort( split(//,$w) ) )} }, $w ) while( cho +mp($w = <FILE>) ); close(FILE); print "enter word and size(q to quit) "; chomp($cont = <STDIN>); while($cont !~ m/^q$/i) { %combs = @combs = %bysize = (); ($word, $size) = split(" ", $cont); if($size =~ m/^\d$/) { $eval = '$combs{$entry} = 1 if ($entry =~ m/^\w{' . $size . '} +$/ )'; } elsif($size =~ m/-/) { my ($fnum, $snum) = $size =~ m/(\d+)\s*-\s*(\d+)/; $eval = '$combs{$entry} = 1 if ($entry =~ m/^\w{' . $fnum . ', +' . $snum . '}$/)'; } elsif($size ne "") { $eval = '$combs{$entry} = 1 if length($entry)' . " $size"; } #find all combinations in word @word = split(//, $word); foreach (&combinations(@word)) { $entry = join("", sort(@$_)); if($size eq "") { $combs{$entry} = 1 if length($entry) > 0; } else { eval $eval; } } @combs = sort(keys %combs); # store each word in bysize hash arranged by size foreach (@combs) { push( @{ $bysize{ length($_) } }, @{ $anagrams{$_} } ) if @{ $ +anagrams{$_} }; } # print words &print_set(); print "enter word and size(q to quit) "; chomp($cont = <STDIN>); } sub print_set() { my ($key, $count, $total); my @words; foreach $key ( sort { $a <=> $b } keys %bysize ) { @words = sort( @{ $bysize{$key} } ); print "\nwords of size $key:\n"; $count = 0; foreach (@words) { $count++; print "$_ "; print "\n" if $count % 7 == 0; } $total += $count; print "\n"; } print "\ntotal words: $total\n"; print "\n\n"; } sub combinations() { return [] unless @_; my $first = shift; my @rest = combinations(@_); return @rest, map { [$first, @$_] } @rest; } exit;
sampe run using the scrabble enable.txt dictionary

D:\PerlProjects\anagram>anagram.pl enable.txt reading dictionary... enter word and size(q to quit) perlmonks 3-5 words of size 3: elk elm els ems ens eon ern ers ken kep kop kor kos lek lop mel men mol mon mop mor mos nom nor nos oes oke ole oms one ons ope ops ore ors ose pen per pes pol pom pro rem rep res roe rom sel sen ser sol son sop words of size 4: elks elms enol eons epos erns eros kelp kemp keno kens keps kern knop koel kops kore kors leks leno lens lone lope lops lore lorn lose mels meno merk merl moke mole mols monk mons mope mops more morn mors mosk noel noes nome noms nope norm nose okes oles omen omer ones open opes ores orle pens peon perk perm peso poem poke pole pols pome poms pone pons pore pork porn pose prom pros rems repo reps roes role romp roms rope rose skep sloe slop soke sole some sone sore sorn words of size 5: enols enorm enrol kelps kemps kenos kerns knops knosp koels krone lemon lenos loner loper lopes lores loser melon merks merls meson mokes moles monks moper mopes morel mores morns morse nerol noels nomes norms omens omers opens orles pelon peons perks perms plonk poems poker pokes poler poles pomes pones pores porks porns poser proem prole proms prone prose repos roles romps ropes senor skelm skelp slope smerk smoke snore sorel sperm spoke spore total words: 223 enter word and size(q to quit) q
The other things the fuller program does is
1) allow for inclusion/exclusion of specific letters
2) allow for returning anagrams that contain a specific regex pattern

If you are interested in seeing that, let me know and I will post the fuller version.

davidj

In reply to Re: Finding words within a character set by davidj
in thread Finding words within a character set by ruhk

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.