• This code seems like a very convoluted way to parse a 10 digit number and put parenthesis, spaces and dashes in the result.

    Why not check for numeric and if true use the substr command to create the result?