Slash Boxes
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)

  (email not shown publicly)

Journal of colomon (8994)

Wednesday December 24, 2008
08:51 PM

Phone Number Script, Improved Version

[ #38162 ]
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....

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