powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way using sprintf, or a Perl function to take a Phone number and make it formatted something like this:
1234567890 would be: (123)456-7890

or even 123-456-7890 would work.
I've looked at the perldoc for sprintf, but did not see anything like it. I would appreciate any tips on this.

thx,
Richard

Replies are listed 'Best First'.
Re: formatted phone number from variable
by thezip (Vicar) on May 22, 2003 at 06:13 UTC
    Try this:
    #!/perl/bin/perl use strict; use warnings; # The assumption is that the number will always be exactly ten digits my $s = "0123456789"; print $s, "\n"; $s =~ s/^(\d{3})(\d{3})(\d{4})$/($1)$2-$3/; print $s, "\n"; __OUTPUT__ 0123456789 (012)345-6789
    Where do you want *them* to go today?
Re: formatted phone number from variable
by Zaxo (Archbishop) on May 22, 2003 at 06:23 UTC

    A regular expression with substitution is one way. Some extras included:

    my $phone = '1234567890'; # convert alpha mnemonics $phone =~ tr/A-PR-Z/222333444555666777888999/; $phone =~ tr/a-pr-z/222333444555666777888999/; # get rid of any nondigits $phone =~ s/\D//g; # format $phone =~ s/^(\d{3})(\d{3})(\d{4})$/{$1) $2-$3/; $phone =~ s/^(\d{3})(\d{4})$/$1-$2/; # no AC print $phone, $/;

    (Added) 'perldoc perlre' is the primary reference. It should be installed with your perl.

    After Compline,
    Zaxo

      Thank you both!

      Can someone explain where I can find information on this?
      Mainly the \d{3} etc.

      What would I search for in the perldocs to read up on it?
      thx,
      Richard

      Added: Thank you for adding the reference. I appreciate it!

        Another great source of information about regular expressions is Mastering Regular Expressions (2nd Ed), by Jeffrey Friedl. This book explains very well how regexes work, and shows how to use them in a variety of languages, not only Perl.

        Arjen

      Just my thoughts about your "convert alpha mnemonics":
      # convert alpha mnemonics $phone =~ tr/A-Za-z/2223334445556667Q7788899992223334445556667q7788899 +99/;
      is a bit shorter ;-) and does it in one run. BTW: On german phones you'll find the Q on 7. So Germans should replace Q and q above with 7.

      Question: Did you forget the last 9 (9999 for wxyz) in your code?

Re: formatted phone number from variable
by htoug (Deacon) on May 22, 2003 at 14:12 UTC
    That has been discussed a number of times on perlmonks before.
    Using Super Search with the words 'phone', 'number' and 'format' gave me an enormous number of hits. I was looking for this node, where there is a good analysis of the problem - and a discussion of the formatting of numbers from different countries.
    You are seeking a too simple solution to the problem - and could have used Super Search to find a solution to a more complete problem.

      While i agree with your suggestion to allways Super Search first...

      Just because there is a more complete problem, doesn't mean a person is interested in a solution to the complete problem -- in many cases the problems people face are simple subsets of larger "complete" problems.

      Regardless of the posters subject, it was very clear that the input would be "1234567890" and the desired output was "(123) 456-7890"

      If I was in a situation like that, where I knew my input would be limited to exactly 10 digits, with no punctuation, and i found node#159645 ... i would consider it severe overkill, and ask the question anyway. (Because I don't care about the obscure phone number rules of various countries ... i care about formating 10 digits)

        This was just my attempt to point out that what may superficially seem to be a simple problem, in fact is not.

        Formatting phone numbers is not simple.
        Formatting 10 digit numbers is simple though.

        The poster did specifically mention phone number in the node, so I took the chance to point out that he might be tackling a problem quite a bit larger than he originally intended. You do have to remember that there is a world outside your local pond, and sometimes it can crawl up on you and disturb your nice schemes.

        If (and only if) you are certain that all the phone numbers you will have to work with (now and in the future) are exactly 10 digits, then the 10 digit formatting assumption holds. I just don't think that the assumption holds - as my phone number is just 8 digits.

Re: formatted phone number from variable
by jaa (Friar) on May 22, 2003 at 15:43 UTC

    Rather than updating the phone number with REs, why not:

    my $phone = '1234567890'; printf("(%s)%s-%s\n", unpack "A3 A3 A*", $phone);
    printf(), sprintf() and unpack() functions are generally considered efficient.
      good solution, but it doesn't take care of the second case that was mentioned, where the phone number could be 1-234-567-890.

      An application I had to make for my office needs to format phone numbers that come in all shapes and sizes... I use this:

      $phone =~ s/.*(\d{3}).*(\d{3}).*(\d{4}).*/$1-$2-$3/;

      which is JAWTDI, similar to others mentioned here. Of coures, checks and balances for when there are not 10 digits apply.


      Jason W.