HL7 to HL7

String munching galore...

For this we’ll need to include two more modules

require 'stringutil'
require 'clean_phone'

The stringutil module we provide has a number of helpful string extension functions. One handy one is called capitalize.

We can use that on the first name and last name of the patient like so:

For fun do have a look at how the capitalize function is implemented; it’s very transparent how the function works:

Now let’s clean up the phone number format: (anyone remember the Hitchhiker’s Guide to the Galaxy?)

You can have fun tweaking the implementation of CleanPhone to get slightly different formats of the phone number:

Try tweaking the code to use square brackets and different spacing. You can see the ease of the Translator environment for this type of data manipulation.

require 'split'
require 'zsegment'
require 'diff'
require 'hl7util'
require 'dateparse'
require('stringutil')
require('clean_phone')

local function trace(a,b,c,d) return end

function main(Data)
   local Orig = hl7.parse  {vmd = 'transform.vmd', data = Data}

   if Orig:nodeName() == 'Catchall' then
      iguana.logInfo('Filtered '..Orig.MSH[9][1]..'^'..Orig.MSH[9][2])
      return
   end

   local Out  = hl7.message{vmd = 'transform.vmd', name = Orig:nodeName()}
   Out:mapTree(Orig)

   local Copy = zsegment.copyZSegments(Data, Out:S())
   CheckTransform(Data:StripLastReturns(), Copy:StripLastReturns())

   AlterMSH(Out.MSH)
   local PID = hl7util.findSegment(Out, FindPID)
   if PID then
      AlterPID(PID)   
   end

   trace(Out)
   local Diff = diff.Compare(Orig:S(), Out:S(), 'transform.vmd')
   local DataOut = Out:S()
   DataOut = zsegment.copyZSegments(Data, DataOut)
   DataOut = DataOut:StripLastReturns()
   trace(DataOut)
   queue.push{data = DataOut}
end

function FindPID(Segment)
   if Segment:nodeName() == 'PID' then
      return true
   end
end

function AlterPID(PID)
   local Date      = dateparse.parse(PID[7]:nodeValue())
   PID[7]          = os.date("%Y%m%d", Date)
   PID[5][1][1][1] = PID[5][1][1][1]:nodeValue():capitalize()
   PID[5][1][2]    = PID[5][1][2]:nodeValue():capitalize()
   PID[13][1][1]   = PID[13][1][1]:CleanPhone()
   return PID
end

function AlterMSH(MSH)
   MSH[3][1] = 'Acme'
   MSH[4][1] = 'Lab'
   return MSH
end

function CheckTransform(Orig, Copy)
   if Orig ~= Copy then
      trace(Orig)
      trace(Copy)
      error('Copy of HL7 message does not match the original')
   end   
end

function string.StripLastReturns(S)
   -- strip return(s) "\r" & "\n" from the end of a string
   local i = #S
   while S:byte(i) == 10 or S:byte(i) == 13 do
      i = i - 1
   end
   return S:sub(1,i)
end

Leave A Comment?

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