in reply to formatted phone number from variable

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.

Replies are listed 'Best First'.
Re: Re: formatted phone number from variable
by apsyrtes (Beadle) on May 22, 2003 at 18:11 UTC
    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.