Working with HL7

Formatting output HL7 without trailing | characters

If you apply the tostring() function (which is how the :S() short cut method is implemented) to a HL7 node tree is will format the output such that:

  • There are trailing “|” characters
  • The default HL7 delimiters are used

Sometimes you may wish to have more control over the output format. The best way to do this is to make a node extension routine which gives you the precise format you need.

Don’t worry, we don’t expect customers to write these, if you run this issue just let us know and we’re happy to put together examples of different routines to do this for you.

This example uses a custom function node.flatwire() to format a message without any trailing “|” characters:

function main(Data)   
   local Msg = hl7.parse  {vmd = 'example/demo.vmd', data = Data}
   -- Out1 contains the message without trailing "|" delimiters
   local Out1 = Msg:flatwire()
   -- Out2 contains the message with the default formatting.
   local Out2 = Msg:S()

function node.flatwire(Node) 
   local R = ''
   for i=1, #Node do
      if Node[i]:nodeType() == 'segment' and not Node[i]:isNull() then
         R = R..Node[i]:S()
         R = R:sub(1, #R-1)  -- get rid of trailing delimiters
         R = R..'\r'
      elseif Node[i]:nodeType() == 'segment_repeated' then
         R = R..Node[i]:flatwire()
      elseif Node[i]:nodeType() == 'segment_group' then
         R = R..Node[i]:flatwire()
   return R

Tip: You can put node.flatwire() in a module like “mynode” or “flatwire” if you wish.

Comparing the output, Out1 will contain the custom format:

MSH|^~\&|AcmeMed|Lab|Main HIS|St. Micheals|20110213144932||ADT^A03|9B38584D9903051F0D2B52CC0148965775D2D23FE4C51BE060B33B6ED27DA820|P|2.6
PID|||4525285^^^ADT1||Smith^Tracy||19980210|F||Martian\E\|86 Yonge St.^^ST. LOUIS^MO^51460|||||||10-346-6|284-517-569
NK1|1|Smith^Gary|Second Cousin

And Out2 contains a standard message:

MSH|^~\&|AcmeMed|Lab|Main HIS|St. Micheals|20110213144932||ADT^A03|9B38584D9903051F0D2B52CC0148965775D2D23FE4C51BE060B33B6ED27DA820|P|2.6|
PID|||4525285^^^ADT1||Smith^Tracy||19980210|F||Martian\E\|86 Yonge St.^^ST. LOUIS^MO^51460|||||||10-346-6|284-517-569|
NK1|1|Smith^Gary|Second Cousin|

Leave A Comment?

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