How to modify selected fields when Segment Grammar unknown

Say we have to modify four fields in PID segment, in a message, but we don’t know message segment grammar, or don’t wish to care about segment grammar 🙂

First step: we will create segment grammar that consists only of two segments: MSH to be able identify a message, and PID to modify required fields. Rest of segment grammar is left free, to be any combination of any segments.

Next we write simple Lua script that modifies four fields in PID segment.

Last we call function copySegments from our custom module named ‘segment’. This function effectively copies rest of yet modified segments preserving original segments order.

segment = {}

function segment.copySegments(Orig, Copy)
   --[[
   Append segments which don't require mappings 
   (from original message)
   into list of already mapped (modified) segments.
   ]]

   local O ={}
   for line in Orig:gmatch("[^r]+") do 
      table.insert(O,line) end

   local C ={}
   for line in Copy:gmatch("[^r]+") do 
      table.insert(C,line) end

   for i = 1, #O do       
      if O[i]:sub(1,3) ~= 'MSH' and 
         O[i]:sub(1,3) ~= 'PID' then 
         table.insert(C, i, O[i])
      end
   end

   local R = ''
   for i = 1, #C do
      R = R..C[i]..'r'
   end
   return R
end

Modify ADT.vmd

Leave A Comment?