Phone Number Script, Improved Version

colomon on 2008-12-25T01:51:38

I just realized that my problem with trans was that I had that extra space between the function call and the paren. With that removed, trans works perfectly, and makes a more elegant version of my script:

my $wordlist_filename = "wordlist.txt";
my $test_numbers_filename = "numbers.txt";

sub Number (Str $s) { my $result = lc($s); $result.=trans('abc' => '2', 'def' => '3', 'ghi' => '4', 'jkl' => '5', 'mno' => '6', 'prs' => '7', 'tuv' => '8', 'wxy' => '9'); return $result; }

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

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

my $test_numbers = open($test_numbers_filename); for (=$test_numbers) -> $number { my $word = %numbers{$number}; say "$number ==> $word"; } close ($test_numbers);


Note that in a comment jj suggests a much more spiffy and Perlish way of doing this, which I may try translating to Perl 6 if I get ambitious....