Jimmy Christian · Dec 27, 2019

Read File with HL7 messages and coun number of FT1

Hello team,

I am trying to do a basic read file of HL7 messages. Need to count how many FT1 segments are there in each file and how many messages are in each file.

Any help will be appreciated. I have the stream object.

Just need to loop through the stream and get the info in some way.

Any help will be appreciated.


Jimmy Christian.

1 437
Discussion (4)0
Log in or sign up to continue

Assuming the stream contains the normal $C(13) segment delimiters:

Class User.HL7.Stream Extends %RegisteredObject

ClassMethod GetCounts(pStream As %Stream.FileCharacter, Output pMsgCount As %Numeric, Output pSegCounts As %ArrayOfDataTypes) As %Status
    Do pStream.Rewind()
    Set pStream.LineTerminator = $C(13)
    Set pSegCounts = 0
    While 'pStream.AtEnd
        Set tLine = pStream.ReadLine()
        // Remove leading control characters
        Set tSeg = $ZSTRIP(tLine,"<C")
        // Get the segment name
        Set tSegName = $EXTRACT(tSeg,1,3)
        If tSegName '= ""
            If '$DATA(pSegCounts(tSegName))
                // We have a new subscript
                Set pSegCounts = pSegCounts + 1
                Set pSegCounts(tSegName) = 1
                Set pSegCounts(tSegName) = pSegCounts(tSegName) + 1
    Set pMsgCount = pSegCounts("MSH")
    Return $$$OK


Call the classmethod with the stream as follows:

Do ##class(User.HL7.Stream).GetCounts(stream,.Msgs,.Counts)

Counts will be subscripted by the segment names found in the message stream. In the above example, you'll find the occurrence count of FT1 segments in Counts("FT1") and the number of messages will be in Counts("MSH"). The value returned in Msgs is the same as Counts("MSH").

That is very helpful. I wasnt aware of this class method or class. Thank you for your assistance. I will use it and let you know if  i encounter any issue.

I also tried using another method where i read the stream object using ReadLine.

Then if the string starts with "MSH" OR "FT1" i increment the count.

In that case i have to use the Terminator character as $C(10) or $C(13) 


Jimmy Christian.

Since the class didn't exist before I wrote it earlier today, you can be forgiven for not knowing about it laugh

Ha ha. Thanks Jeffrey.

Yes the class name User.HL7.Stream seemed like the system defined class.