I might have tried to generalize that even further, though you still have regional issues to worry about. I.e. this won't work for everyone:
@FORMAT_PHONE = (
'%d%d%d', # 911
'%d-%d%d%d', # 1-411
'%d-%d%d%d%d', # 5-1212 ?
'%d%d-%d%d%d%d', # 55-1212 ?
'%d%d%d-%d%d%d%d', # 555-1212
'%d-%d%d%d-%d%d%d%d', # 1-222-2345
'%d%d%d-%d%d%d-%d%d%d', # 212-114-151 ?
'(%d%d%d) %d%d%d-%d%d%d%d', # (800) 555-1212
'+%d (%d%d%d) %d%d%d-%d%d%d%d', # +1 (800) 555-1212
'+%d%d (%d%d%d) %d%d%d-%d%d%d%d', # +01 (800) 555-1212
'+%d%d%d (%d%d%d) %d%d%d-%d%d%d%d', # +011 (800) 555-1212
'+%d%d%d %d (%d%d%d) %d%d%d-%d%d%d%d', # +011 1 (800) 555-1212
'%d%d%d%d%d (%d%d%d) %d%d%d-%d%d%d%d', # 10321 (800) 555-1212
'%d%d%d%d%d %d (%d%d%d) %d%d%d-%d%d%d%d', # 10321 1 (800) 555-1212
'%d%d%d%d-%d%d%d (%d%d%d) %d%d%d-%d%d%d%d', # 1010-220 (800) 555-12
+12
'%d%d%d%d-%d%d%d %d (%d%d%d) %d%d%d-%d%d%d%d', # 1010-220 1 (800) 5
+55-1212
# etc...
);
my %L2M;
@L2M{qw: Q Z :} = 1;
@L2M{qw: A B C :} = 2;
@L2M{qw: D E F :} = 3;
@L2M{qw: G H I :} = 4;
@L2M{qw: J K L :} = 5;
@L2M{qw: M N O :} = 6;
@L2M{qw: P R S :} = 7; # some define this as PQRS
@L2M{qw: T U V :} = 8;
@L2M{qw: W X Y :} = 9; # some define this as WXYZ
sub format_phone {
my $number = shift;
$number =~ s/[a-zA-Z]/$L2M{uc($1)}/eg;
$number =~ s/\D//g; # maybe allow # and * later??
if (defined($FORMAT_PHONE[length($number)-3])) {
return sprintf($FORMAT_PHONE[length($number)-3], split(//, $numb
+er));
}
return;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.