The problem is to convert a seven digit phone number to a seven letter word from the dictionary they provide. Trying to generate all the possible words from a given number seems too much like work to me, so I'm looking at the problem the other way around -- each seven letter word in the dictionary generates a unique number. I'll put those in a hash, and then looking up a word for a number will be easy. At least, that's my plan.
I didn't think of creating this journal until I'd worked on the problem a bit, so I don't have my earliest missteps handy. My first effort was just to read the wordlist file and find all the seven letter words. (BTW, this is with Parrot fetched from Subversion last night, and I suppose it's possible I borked my install because I had a previous version of Parrot (from tarball) installed on my machine.) It took a bit of playing around to figure out the new syntax for this sort of thing. The first big roadblock I hit was that I couldn't get the
readline function to work for me.
=$filehandle works instead, so that wasn't a show stopper.
My next attempt was to figure out a way to check if a string had exactly seven letters. It looked to me like
.elems was the way to do that in P6, but it always seemed to return 1. (Possibly it was treating it as an array with one string in it, rather than a string with N characters in it?)
.codes didn't seem to be recognized at all.
I finally solved this with a clumsy but working regex:
if ($word ~~.
Then I worked on a sub to convert the input word to a number. New sub parameter syntax, yay! Laziness kicking in again, I figured good old tr was the easiest way to do the conversion. So far this one has me stumped, however. I haven't found a syntax for tr that Raduko likes yet.
Here's my current code:
my $wordlist_filename = "wordlist.txt";
sub Number (Str $s)
$s.=trans ('abc' => '2', 'def' => '3');
# $s ~~ tr/abcdefghijklmnopqrstuvwxyz/222333444555666777888999/;
my $wordlist = open($wordlist_filename);
# err die "Could not open $wordlist: $!\n";
for (=$wordlist) -> $word
if ($word ~~
my $number = Number($word);
say "$word ==> $number";
(Also note that
err die failed to compile, which is why it is commented out.)