Here's my take on this problem, using Progress ABL, external .p file so that it could be run from various data directives. There are a lot of tables with phone numbers!
/*
Procedure FormatPhone.p - Given phone string 1234567890[x123...], validate and return it formatted.
Usage:
run FormatPhone.p (INPUT vcphone, OUTPUT vcPhone2) NO-ERROR.
if error-status:error then do:
/* error handling code, message to user, etc. */
end.
*/
def input param vcPhone as char no-undo.
def output param vcPhone2 as char no-undo.
def var vcExt as char no-undo.
def var vdTemp as decimal no-undo.
def var vcbad as char no-undo.
def var delim as char no-undo.
def var badchars as char no-undo initial "-().". /* add others if needed but leave "X" to delimit the extension */
def var i as int no-undo.
do i = 1 to length(badchars):
vcBad = substring(badchars, i, 1).
vcphone = replace(vcPhone, vcBad, "").
end.
if length(vcPhone) > 10 then do:
/* multiple character delim didn't work, convert to "x" */
if index(vcPhone, "Extension") > 0 then vcPHone = replace(vcPhone, "Extension", "x").
else if index(vcPhone, "Ext") > 0 then vcPhone = replace(vcPhone, "Ext", "x").
else if index(vcPhone, "Ex") > 0 then vcPhone = replace(vcPhone, "Ex", "x").
if index(vcPhone, "x") > 0 then delim = "x".
if delim > ""
then assign
vcExt = trim(entry(2, vcPhone, delim))
vcPhone = trim(entry(1, vcPhone, delim))
vcExt = replace(vcExt, ".", "")
vcPhone = replace(vcPhone, Delim, ""). /* strip out the "X" */
end.
if vcPhone > "" and length(vcPhone) <> 10 then return error "Phone# must be 10 numeric digits".
assign vdTemp = DECIMAL(vcPhone) no-error.
if error-status:error then do: /* not an integer */
return error "Non-numeric digits in phone number: " + vcPhone.
end.
else do:
vcPhone2 = string(vcPhone,"(999)999-9999") + (if vcExt > "" then "x" + vcExt else "").
end.
Thanks!
Christopher Heins
Sr. Progress Programmer/Analyst
[cid:image001.png@01CC2F36.1A6D6C60]
Cell# 908-256-3662
cheins@...<mailto:cheins@...>
________________________________
NOT INTENDED AS A SUBSTITUTE FOR A WRITING
NOTHING IN THIS E-MAIL, IN ANY E-MAIL THREAD OF WHICH IT MAY BE A PART, OR IN ANY ATTACHMENTS THERETO, SHALL CONSTITUTE A BINDING CONTRACT, OR ANY CONTRACTUAL OBLIGATION BY PNY, OR ANY INTENT TO ENTER INTO ANY BINDING OBLIGATIONS, NOTWITHSTANDING ANY ENACTMENT OF THE UNIFORM ELECTRONIC TRANSACTIONS ACT, THE FEDERAL E-SIGN ACT, OR ANY OTHER STATE OR FEDERAL LAW OF SIMILAR SUBSTANCE OR EFFECT. THIS EMAIL MESSAGE, ITS CONTENTS AND ATTACHMENTS ARE NOT INTENDED TO REPRESENT AN OFFER OR ACCEPTANCE OF AN OFFER TO ENTER INTO A CONTRACT. NOTHING IN THIS E-MAIL, IN ANY E-MAIL THREAD OF WHICH IT MAY BE A PART, OR IN ANY ATTACHMENTS THERETO SHALL ALTER THIS DISCLAIMER.
This e-mail message from PNY Technologies, Inc. is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.
______________________________________________________________
[Non-text portions of this message have been removed]
/*
Procedure FormatPhone.p - Given phone string 1234567890[x123...], validate and return it formatted.
Usage:
run FormatPhone.p (INPUT vcphone, OUTPUT vcPhone2) NO-ERROR.
if error-status:error then do:
/* error handling code, message to user, etc. */
end.
*/
def input param vcPhone as char no-undo.
def output param vcPhone2 as char no-undo.
def var vcExt as char no-undo.
def var vdTemp as decimal no-undo.
def var vcbad as char no-undo.
def var delim as char no-undo.
def var badchars as char no-undo initial "-().". /* add others if needed but leave "X" to delimit the extension */
def var i as int no-undo.
do i = 1 to length(badchars):
vcBad = substring(badchars, i, 1).
vcphone = replace(vcPhone, vcBad, "").
end.
if length(vcPhone) > 10 then do:
/* multiple character delim didn't work, convert to "x" */
if index(vcPhone, "Extension") > 0 then vcPHone = replace(vcPhone, "Extension", "x").
else if index(vcPhone, "Ext") > 0 then vcPhone = replace(vcPhone, "Ext", "x").
else if index(vcPhone, "Ex") > 0 then vcPhone = replace(vcPhone, "Ex", "x").
if index(vcPhone, "x") > 0 then delim = "x".
if delim > ""
then assign
vcExt = trim(entry(2, vcPhone, delim))
vcPhone = trim(entry(1, vcPhone, delim))
vcExt = replace(vcExt, ".", "")
vcPhone = replace(vcPhone, Delim, ""). /* strip out the "X" */
end.
if vcPhone > "" and length(vcPhone) <> 10 then return error "Phone# must be 10 numeric digits".
assign vdTemp = DECIMAL(vcPhone) no-error.
if error-status:error then do: /* not an integer */
return error "Non-numeric digits in phone number: " + vcPhone.
end.
else do:
vcPhone2 = string(vcPhone,"(999)999-9999") + (if vcExt > "" then "x" + vcExt else "").
end.
Thanks!
Christopher Heins
Sr. Progress Programmer/Analyst
[cid:image001.png@01CC2F36.1A6D6C60]
Cell# 908-256-3662
cheins@...<mailto:cheins@...>
________________________________
NOT INTENDED AS A SUBSTITUTE FOR A WRITING
NOTHING IN THIS E-MAIL, IN ANY E-MAIL THREAD OF WHICH IT MAY BE A PART, OR IN ANY ATTACHMENTS THERETO, SHALL CONSTITUTE A BINDING CONTRACT, OR ANY CONTRACTUAL OBLIGATION BY PNY, OR ANY INTENT TO ENTER INTO ANY BINDING OBLIGATIONS, NOTWITHSTANDING ANY ENACTMENT OF THE UNIFORM ELECTRONIC TRANSACTIONS ACT, THE FEDERAL E-SIGN ACT, OR ANY OTHER STATE OR FEDERAL LAW OF SIMILAR SUBSTANCE OR EFFECT. THIS EMAIL MESSAGE, ITS CONTENTS AND ATTACHMENTS ARE NOT INTENDED TO REPRESENT AN OFFER OR ACCEPTANCE OF AN OFFER TO ENTER INTO A CONTRACT. NOTHING IN THIS E-MAIL, IN ANY E-MAIL THREAD OF WHICH IT MAY BE A PART, OR IN ANY ATTACHMENTS THERETO SHALL ALTER THIS DISCLAIMER.
This e-mail message from PNY Technologies, Inc. is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.
______________________________________________________________
[Non-text portions of this message have been removed]