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; }

In reply to Re: Answer: How do I break down a phone number and rebuild it? by Fastolfe
in thread How do I parse a telephone number? by mofo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.