Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Jumble solver

by gmax (Abbot)
on Dec 18, 2003 at 19:27 UTC ( #315626=note: print w/replies, xml ) Need Help??


in reply to Jumble solver

An alternate method, without external programs, using a method similar to the one seen in Perl's pearls:

#!/usr/bin/perl -w use strict; use DB_File; my $search_word = shift or die "word required"; my %sorted; tie %sorted, 'DB_File', "words_jumble.db"; unless (keys %sorted) { # Creates the word index. open WORDS, "/usr/share/dict/words" # your favorite dictionary h +ere or die "can't open words file\n"; while (<WORDS>) { chomp; my $word = pack "C*", sort unpack "C*", lc $_; $sorted{$word} .= "," if $sorted{$word}; $sorted{$word} .= $_; } close WORDS; } my $sorted_word = pack "C*", sort unpack "C*", lc $search_word; die "$search_word not found\n" unless exists $sorted{$sorted_word}; print "[$_]\n" for split",", $sorted{$sorted_word}; __END__ perl jumble.pl elvis [Elvis] [evils] [Levis] [lives] [veils] perl jumble.pl parse [pares] [parse] [pears] [rapes] [reaps] [spare] [spear] perl jumble.pl steak [Keats] [skate] [stake] [steak] [takes]
 _  _ _  _  
(_|| | |(_|><
 _|   

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://315626]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2022-12-06 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?