in reply to formatted phone number from variable

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

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

Re: Re: formatted phone number from variable
by Skeeve (Parson) on May 22, 2003 at 07:56 UTC
    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?