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

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.
  • I don't really know any Perl 6 syntax, but this was my Perl 5 version:

    #! /Users/jj/bin/perl5.10.0

    use strict;
    use warnings;
    use 5.010;

    say "Please enter a phone number:";
    my $number = <>;
    chomp $number;

    $number =~ s/2/[ABC]/g;
    $number =~ s/3/[DEF]/g;
    $number =~ s/4/[GHI]/g;
    $number =~ s/5/[JKL]/g;
    $number =~ s/6/[MNO]/g;
    $number =~ s/7/[PRS]/g;
    $number =~ s/8/[TUV]/g;
    $number =~ s/9/[WXY]/g;

    open WORDLIST,'<','wordlist.txt' or die "Cannot open wordlist.txt: $!";

    [<WORDLIST>] ~~ /^($number)\s*$/i and say uc $1;


    This converts the number into a regex, e.g. "42" would become "[GHI][ABC]" and then uses a smart match to go through the list of words.

    Would this approach look any different in Perl 6? (it would be good if there was a way to smart match against the filehandle without having to load it).

    • Ha! That's brilliant.

      I've no idea if that basic approach will work in current Perl 6. I may give it a try if I get a chance...