Question
· Aug 30, 2019

How to get number of segments in a hl7 message and then pull a field value from the last segment

Hi, Is there a way to count the number of segments in a HL7 Mesage? I tried the examples from the other answers but nothing works for me.

I am writing a function to get the last OBX segment field value 5. Below is a sample screenshot of what I want.

The number of obx segments can change, so I want to count the last obx segment and then get the field 5 value.

Would appreciate some guidance on this.

Discussion (10)1
Log in or sign up to continue

Have a look at this documentation on Virtual Property Path syntax.

Essentially, if you specify an asterisk "*" inside the parentheses for a repeating segment it will return a count of how many of those segments exist in the document.

<assign value='source.{PID:3("*")}' property='pid3Count'/>

In this example, the number of repetitions that exist in PID:3 will be stored in the variable pid3Count.

You could then use source.{PID:3(pid3Count)} to refer to the last item in PID:3.

Hi Marc, thank you so much. Actually I had to edit my question. My aim is to get the 5th field value from the last OBX segment in my HL7 message. Because I can have varying number of OBX segments but I know that the value I want is in the last OBX segment. 

So I thought if I could get the number of segments in a message, and then index that to get the last segment I could get the value I want.

Is there a better way to do that?

Sorry for the confusion. Appreciate your help