Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

colomon (8994)

colomon
  (email not shown publicly)

Journal of colomon (8994)

Wednesday December 24, 2008
05:04 AM

Messing Around With Scripting Games

[ #38150 ]
So, I dived straight in with "Advanced Event 1: Could I Get Your Phone Number?" I messed around with Pugs a bit a few years back, but this is my first real attempt to do something with Raduko.

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?) .bytes and .codes didn't seem to be recognized at all.

I finally solved this with a clumsy but working regex: if ($word ~~ /^\w\w\w\w\w\w\w$/).

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/;
    return $s;
}

my $wordlist = open($wordlist_filename);
    # err die "Could not open $wordlist: $!\n";

for (=$wordlist) -> $word
{
    if ($word ~~ /^\w\w\w\w\w\w\w$/)
    {
        my $number = Number($word);
        say "$word ==> $number";
    }
}

close ($wordlist);

(Also note that err die failed to compile, which is why it is commented out.)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.