Cleaning up phone numbers

This routine formats phone numbers nicely with the area code in brackets:

-- following best practice by putting local
-- functions first before main()

local function CleanPhoneNumber(PN)
  local Result
  if PN then
     local NumAsString = PN:nodeValue()
     -- Remove all non-numeric characters from the phone number.
     local N = NumAsString:gsub('[^%d]', '')
     if #N == 10 then
        Result =  '('..N:sub(1,3)..')'..N:sub(4,6)..'-'..N:sub(7,10)
     elseif #N == 7 then
        Result = N:sub(1,3)..'-'..N:sub(4,7)
     end
  else
     Result = nil
  end
  return Result
end

function main(Data)
   local msg = hl7.parse{vmd = 'example/demo.vmd', data = Data}
   
   local cleanHmPhone = CleanPhoneNumber(msg.PID[13][1][1]) -- home phone
   local cleanWkPhone = CleanPhoneNumber(msg.PID[14][1][1]) -- business
end

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.