Normalize X12 delimiters

Verified
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
Usage Details

The Translator X12 module expects standard delimiters to be present when parsing message data. This code shows how to “normalize” non-standard delimiters prior to processing. You can revert the delimiters after processing if desired (this is not shown in the code).

Note: The sample code is very similar to one of Lev’s tips: How to ‘normalize’ X12 delimiters prior to processing content.

How to use the code:

  • Download the 270BlueCrossEdited.edi file
    Note: You will need to remove the “.txt” extension
  • Modify the MY_FILE variable (at the start of the code) to point to where you saved 270BlueCrossEdited.edi
  • Paste the code into a Filter or To Translator script
  • Examine the code and annotations to see how it works