Formatting a blob of text data into a single segment with a repeating field or multiple segments

This post was originally written for Iguana 5 so it contains version 5 screenshots, and may contain out of date references.

This is a very common problem in HL7. Some systems expect textual reports to be formatted into an OBX or NTE segment with a repeating field, while others want each line of the report in its NTE. For instance:

MSH|^~&|
NTE|||Patient's head appeared to be potatoe shaped.~Spoke of needing to get back to the other~toys and mentioned Woody.|

or

MSH|^~&|
NTE|||Patient's head appeared to be potatoe shaped.|
NTE|||Spoke of needing to get back to the other|
NTE|||toys and mentioned Woody.|

Both forms can be easily supported using the Translator. Here is a rough example:

Here’s a copy-paste-able snippet:

local Report = "Patient's head appeared to be potatoe shaped.\nSpoke of needing to get back to the other\ntoys and mentioned Woody."

local UseOneNte = true

function main(Data)
   local Out = hl7.message{vmd='example/demo.vmd', name='Lab'}
   local Lines = Split(Report, "\n")
   print("There are "..#Lines.." lines in the report.")

   if (UseOneNte) then
      for i=1, #Lines do
         Out.NTE[1][3][i] = Lines[i]   
      end
   else 
      for i=1, #Lines do
         Out.NTE[i][3][1] = Lines[i]   
      end
   end
   Out:S()
end

function Split(str, delim, maxNb)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then
        return { str }
    end
    if maxNb == nil or maxNb < 1 then
        maxNb = 0    -- No limit
    end
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gfind(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if nb ~= maxNb then
        result[nb + 1] = string.sub(str, lastPos)
    end
    return result
end

Leave A Comment?