[email protected] wrote:
> 
> Cheap and dirty, cheezy, kindergarteny, but it does what you want:
> 
> use strict;
> use warnings;
> 
> my $phnum = '847-VICTORY';
> 
> print "before: \"$phnum\"\n";
> 
> $phnum =~ s/[ABC]/2/ig;
> $phnum =~ s/[DEF]/3/ig;
> $phnum =~ s/[GHI]/4/ig;
> $phnum =~ s/[JKL]/5/ig;
> $phnum =~ s/[MNO]/6/ig;
> $phnum =~ s/[PQRS]/7/ig;
> $phnum =~ s/[TUV]/8/ig;
> $phnum =~ s/[WXYZ]/9/ig;
> 
> # add hyphen
> $phnum =~ s/-(\d\d\d)(\d\d\d\d)/-$1-$2/;
> 
> print "after: \"$phnum\"\n";
> 
> 
> before: "847-VICTORY"
> after: "847-842-8679"

Brute force method. :)  But it won't work if there are () or
no - or 2 -'s etc.  tr is much more efficient than your multiple
subs.

Logically you would want to :
1) remove all legal punctuation (and a leading 1 if present),
2) convert all alphas to their numeric equivalent,
3) reject anything that isn't 10 digits (possibly also 7 if
    local numbers are allowed),
4) format the result the way you like.
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to