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
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
Let the regex engine do the work... (Score:1)
#!
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>] ~~
__END__
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).
JJ
Reply to This
Re: (Score:1)
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...