• Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket
iNTERFACEWARE Help Center
  • Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket

Code Repository

Home›Code Repository›hl7.zsegment.lua
Modules

hl7.zsegment.lua

Verified Featured
Added by iNTERFACEWARE

Generic Z segment parser. Parses Z segments without needing grammar definitions in the VMD file.

Source Code
-- Generic Z segment parser
-- http://help.interfaceware.com/kb/generic-z-segment-parser

hl7.zsegment = {}

local function ParseDelim(Data, DelimArray, Index, Compact)
   if Index == 0 then
      return Data
   end
   local Children = Data:split(DelimArray[Index])
   local Result = {}
   if #Children > 1  then
      for i =1, #Children do
         Result[i] = ParseDelim(Children[i], DelimArray, Index-1, Compact)   
      end
   else
      if Compact then
         Result = ParseDelim(Data, DelimArray, Index-1, Compact)
      else
         Result[1] = ParseDelim(Data, DelimArray, Index-1, Compact)
      end
   end
   
   return Result
end

local function AddZSegment(List, Segment, Compact)
   local Fields = Segment:split('|')
   local SegmentName = Fields[1]
   for i=2, #Fields do 
      Fields[i-1] = ParseDelim(Fields[i], {'&','^','~'}, 3, Compact)
   end
   if not List[SegmentName] then
      List[SegmentName] = {} 
   end
   List[SegmentName][#List[SegmentName]+1] = Fields
end

function hl7.zsegment.parse(T)
   local Segments = T.data:split("\r")
   local ZSegments = {}
   for i = 1,#Segments do
      if Segments[i]:sub(1,1) == 'Z' then
         AddZSegment(ZSegments, Segments[i], T.compact)
      end
   end
   return ZSegments
end

local HELP_DEF=[[{
   "Desc": "Parses an HL7/EDI/X12 message and extracts Z segments which it returns in a Lua table. 
   <p>This module gives us two parsing options: \"Expanded\" (compact=false) which pushes all the
   data down to the leaf level, \"Compact\" (compact=true) where we keep the data at a higher 
   level (this assumes there are no sub fields and repeating fields) 
   <p>Using the \"Expanded\" mode is safer as it gives consistent results for messages with/without
   sub-fields and repeating fields. \"Compact\" mode however will fail (give different results) for
   messages with/without sub-fields and repeating fields. <p>If you have optional sub-fields or 
   repeats then you need to use the \"Expanded\" mode.",
   "Returns": [
      {
         "Desc": "A lua table with the Z segments parsed out <u>table</u>."
      }
   ],
   "SummaryLine": "Parses an HL7/EDI/X12 message for Z-zegments without a vmd.",
   "SeeAlso": [
      {
         "Title": "hl7.zsegment.lua - in our code repository.",
         "Link": "http://help.interfaceware.com/code/details/hl7-zsegment-lua"
      },
      {
         "Title": "Z Segment Parser.",
         "Link": "http://help.interfaceware.com/v6/z-segment-parser"
      }
   ],
   "Title": "hl7.zsegment.parse",
   "Usage": "hl7.zsegment.parse{data=<value>, compact=<true|false>}",
   "Parameters": [
      {
         "data": {
            "Desc": "A message to be parsed <u>string</u>. "
         }
      },
      {
         "compact": {
            "Desc": "If <b>false</b> then the parsed tree will push all data to the lowest level <u>boolean</u>. "
         }
      }
   ],
   "Examples": [
      "<pre>local Msg = hl7.zsegment.parse{data=Data, compact=false}</pre>"
   ],
   "ParameterTable": true
}]]

help.set{input_function=hl7.zsegment.parse, help_data=json.parse{data=HELP_DEF}}
Description
Generic Z segment parser. Parses Z segments without needing grammar definitions in the VMD file.
Usage Details

This module parses Z segments without defining a grammar. Without a grammar it’s difficult to know if a given field contains subfields, sub sub fields, is repeating etc. Because of this the parser can be invoked in two modes using the boolean compact flag.

  • In non compact mode all field data is pushed down to the bottom most leaf nodes
  • In compact mode if a field doesn’t have repeats, sub sub fields then the parser optimistically assumes there are no such fields

How to use the code:

  • Add the module to a Filter,  To Translator or LLP script
  • Use the hl7.zsegment.parse{} function to parse Z segments

Here is some sample code for main():

-- The parser gets inserted into the built in "hl7" namespace.
require 'hl7.zsegment'

-- This module is useful if you'd like to be able to parse Z segments without going through the trouble of editing a VMD file
-- https://help.interfaceware.com/kb/generic-z-segment-parser for more information

local Xml=[[
<Kin firstName='' lastName=''/>
]]

-- Because we don't know the grammar of the segments this module gives us the choice of pushing all the data
-- down to the lowest level leaf (compact=false)  This is safest but makes for verbose paths
-- Or we can keep the data at a higher level (assuming there are not sub fields and repeating fields)
function main(Data)
   -- This shows the compact parsing mode
   local CompactZED = hl7.zsegment.parse{data=Data, compact=true}   
   local X = xml.parse{data=Xml}
   X.Kin.firstName = CompactZED.ZID[1][2][2][1]
  
   -- This shows the non compact parsing mode
   local ExpandedZED = hl7.zsegment.parse{data=Data, compact=false}
   X.Kin.firstName = ExpandedZED.ZID[1][2][2][1][1]
end
More Information
A generic Z segment parser
Bookmark
  • Reviews
  • Related Listings
Filter
Sort by: Rating
  • Newest First
  • Oldest First
  • Helpfulness
Write a Review
Rating
Keyword
Filter
Sort by: Highest Rated
  • Newest First
  • Oldest First
  • Title
  • Most Reviews
Rating
iNTERFACEWARE
urlcode.lua
Added by iNTERFACEWARE
Modules
A module for parsing URL encoded GET/POST sequences
xml.lua
Added by iNTERFACEWARE
Modules
A collection of helpful XML node functions.
dup.lua
Added by iNTERFACEWARE
Modules
Duplicate message filter.
stringutil.lua
Added by iNTERFACEWARE
Modules
A library of helpful extensions to the standard Lua string library.
store.lua
Added by iNTERFACEWARE
Modules
The "original" store module: Allows you to store key/value pairs in a persistent storage mechanism. We recommend using the new store2 module instead.
sha1.lua
Added by iNTERFACEWARE
Modules
A pure Lua-based implementation of the popular SHA-1 hashing function.
retry.lua
Added by iNTERFACEWARE
Modules
A module for retrying operations which might periodically fail like database operations.
mime.lua
Added by iNTERFACEWARE
Modules
Sends MIME-encoded email attachments using the SMTP protocol. A wrapper around net.smtp.send.
iguanaServer.lua
Added by iNTERFACEWARE
Modules
Provides programmatic access to various operations that can be performed on Iguana channels.
hl7.findSegment.lua
Added by iNTERFACEWARE
Modules
A utility for finding any HL7 segment in a parsed HL7 message node tree.
dateparse.lua
Added by iNTERFACEWARE
Modules
A fuzzy date/time parser that is very useful for automatically translating a wide variety of date/time formats.
Showing 21 - 31 of 31 results
«12»

Topics

  • expandGetting Started
  • expandAdministration
    • expandInstallation
    • expandLicensing
    • expandUpgrades
    • expandDeployment
    • expandConfiguration Management
      • expandCustom Configuration
    • expandBackup and Restore
    • expandSecurity
      • expandHIPAA Compliance
    • expandTroubleshooting
  • expandDeveloping Interfaces
    • expandArchitecture
    • expandInterfaces
      • expandHL7
      • expandDatabase
        • expandConnect
      • expandWeb Services
      • expandCDA
      • expandX12
      • expandOther Interfaces
      • expandUtilities
    • expandRepositories
      • expandBuiltin Repositories
        • expandIguana Upgrade
        • expandIguana Tutorials
        • expandIguana Tools
        • expandIguana Protocols
        • expandIguana Files
        • expandIguana Date/Time
        • expandIguana Webservices
        • expandIguana Excel
      • expandRemote Repositories
      • expandCS Team Repositories
        • expandIguana Channels
    • expandSample Code
      • expandModules
      • expandUsing built-in functions
      • expandWorking with XML
    • expandLua Programming
    • expandPerformance
  • expandFAQs and TIPs
    • expandFrequently Asked Questions
      • expandInstalls and Upgrades
      • expandWeb Services
      • expandConfiguration
      • expandChannels
      • expandTranslator
      • expandOther
      • expandDatabase
      • expandAdministration
      • expandLogs
      • expandChameleon
    • expandTips
      • expandChannels
      • expandChameleon
      • expandWeb Services
      • expandSecurity
      • expandProgramming
      • expandOther
      • expandAdministration
  • expandReference
    • expandIguana Enterprise and Professional
    • expandProgram Settings
    • expandChannel Settings
    • expandDashboard
    • expandChannels
    • expandTranslator
    • expandLogs
      • expandLog Encryption
    • expandHTTP API
    • expandCDA API
    • expandError Messages
    • expandChameleon
    • expandIguana Change Log

Other Links

  • Training Center
  • News & Announcements
  • iNTERFACEWARE Blog
  • Older Documention (IGUANA v4 & Chameleon)
Copyright © iNTERFACEWARE Inc.