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