Normalize X12 delimiters

Added by iNTERFACEWARE

How to normalize X12 delimiters for processing in Iguana scripts, they can be reverted after processing if needed

Source Code
-- use the path to where you saved the file "270BlueCrossEdited.edi"
local MY_FILE = '/Users/<some-user>/Downloads/temp/270BlueCrossEdited.edi'

function normalizeDelimiters(d)
   local function ST() -- segment terminator
      if d:find('GS') == 107 then
         return true
      end
      return false
   end
   
   local function CES() -- component element separator
      if d:sub(105,105) == ':' then
         return true
      end
      return false
   end
   
   local function DES() -- data element separator
      if d:sub(4,4) == '*' then
         return true
      end
      return false
   end
   
   if ST() then
      d = d:gsub(d:sub(106,106),'~')
   else
      d = d:gsub(d:sub(106,108),'~')
   end
   
   if not CES() then d = d:gsub(d:sub(105,105),':') end
   if not DES() then d = d:gsub(d:sub(4,4),'*') end
   
   return d
end

function main()
   
   local function readMyFile(fn)
      local f = assert(io.open(fn, 'rb'))
      local d = f:read('*a')
      f:close()
      return d
   end
   
   local Data = readMyFile(MY_FILE)
   
   -- Match message delimiters to standard values
   Data = normalizeDelimiters(Data)
   
   local In,Name,Warnings = x12.parse{
      vmd = 'example/270v3.vmd',
      data = Data}
   
   trace(In)
   
end
Description
How to normalize X12 delimiters for processing in Iguana scripts, they can be reverted after processing if needed