Health Level 7 (HL7) with Perl
Written by Nikos Vaggalis   
Monday, 25 July 2016
Article Index
Health Level 7 (HL7) with Perl
HL7 Grammar Notation
Implementation in Perl
Internal representation

HL7 Grammar Notation

By convention, a standard grammar is defined to specify which segments, and under which conditions, may be included with each type of HL7 message.

adt.notationTable 3: Segment Grammar Notation                                         

The single letter values in the headers of (Tables 1,2), mark Optionality, Repeatability and Cardinality:

R - "Required"
O - "Optional"
* - "Repeating"

Its counterparts in Segment Grammar Notation (Table 3) would assume the following shape :

No brackets around it - "Required"
[]    - "Optional"
{ }   - "Repeating"
[{ }] - "Optional Repeating"

The segment's fields

Each segment occupies one line, and each segment is separated from the following segment with a <Carriage Return>. Segments are subsequently split into fields and use the following encoding/delimiter characters :

|              Field separator, aka pipe
^             Component separator, aka hat
&            Sub-component separator
~             Field repeat separator
\              Escape character

The message always starts with the segment MSH, Message Header.
Let's deconstruct it using the definition in Table 4.

msh - message header hl7 v2.6 - 2016-07-10 17.41.00

Table 4: MSH Segment
Caristix HL7 Definition   


MSH's field1 and 2 (|^~\&) set the delimiters that will be used for the encoding of the message and while they can be customized it is a practice not recommended.

Going through MSH's structure (Table 4) we read that fields MSH.3, MHS.4, MHS.5, MHS.6 are correlated to the Sending Application, Sending Facility, Receiving Application and Receiving Facility, which are empty to indicate that there's no related data and since they're also marked as 'O'ptional, they can be skipped .

What can't be is skipped is the 'R'equired field MSH.7 that marks the Date/Time Of Message. We can find out the format it conforms to by looking into the field's data type DTM. DTM specifies a point in time using a 24-hour clock notation or YYYYMMDDHHMM[SS], with the "seconds" portion being optional.

Field MSH.9 is filled with the Message Type ADT^A01^ADT_A01 and is of type MSG

MSG.1 R Message Code ADT
MSG.2 R Trigger Event A01
MSG.3 R Message Structure ADT_A01

             Table 5: MSG data type

It is also a composite field, made evident by the use of the ^  Component separator that splits it into its three sub-components. If we could picture its contents as an abstract array structure we would find  this:

      MSH.9-1 -  MSG.1 - ADT
      MSH.9-2 -  MSG.2 - A01
      MSH.9-3 -  MSG.3 - ADT_A01

MSH.10 or id210 is a field that contains the unique id of the message. 

MSH.11 is the Processing ID and takes one of the following values:
  P = Production, D = Debugging, T = Training.

MSH.12 or 2.6 identifies the HL7 version of the message.

MSH.22 or DD015 is the internal code of the Sending Organization. 

PID Segment

pid - patient identification hl7 v2.6 - 2016-07-14 13.14.07

Table 6: PID Segment

Caristix HL7 Definition


As seen from the chart, field PID.3 is both Required and Repeatable. Repeatability is denoted by the use of the ~ character, meaning that the field's structure can have multiple instances with different values: 



Furthermore the field's position no 4 (PID.3-4) marks the 'Assigning Authority':

which itself is a composite structure of type HD.  What the & character does is split PID.3-4 into subcomponents according to HD: 

HD.1 20 O Namespace Id


HD.2 999 C Universal Id 2.16.840.1.113883.19.3
HD.3 6 C Universal Id Type ISO
Table 7: HD data type  

So summing up the structure of PID:

    PID.3.1                 100660325
            PID.3-4-1     NationalNP
            PID.3-4-2     2.16.840.1.113883.19.3
            PID.3-4-3     ISO
    PID.3.5 0

    PID.3.1              80253
    PID.3-4             empty
    PID.3-5             1  


DG1 Segment  


dg1 - diagnosis hl7 v2.6 - 2016-07-14 21.10.24

Table 8: DG1 Segment
Caristix HL7 Definition

This is a repeatable segment, thus can appear two or more times:


S42.1 and S42.2 are ICD-10-CM Diagnosis Codes that map into:
Injury, poisoning and certain other consequences of external causes S00-T88 >   Injuries to the shoulder and upper arm S40-S49 > S42.1 Fracture of scapula, S42.2 Fracture of upper end of humerus.

icd-10-cm codes from - 2016-07-11 08.52.59

                                       ICD-10 Diagnosis Codes

PV1 Segment 

pv1 - patient visit hl7 v2.6 - 2016-07-14 21.11.00


Table 9: PV1 Segment

Caristix HL7 Definition


This has straightforward mapping:

PV1.2      Patient Class             E
PV1.7      Attending Doctor      12345678901
PV1.19    Visit Number            185
PV1.20    Financial Class          0

Last Updated ( Wednesday, 27 July 2016 )