Sidebar

How do I properly remove segments using a for loop?

0 votes
901 views
asked Apr 6, 2015 by brandon-w-8204 (33,270 points)

When I remove a segment with a for loop it is not removing some of the segments.

 

Here is the code that is not working:

 

// Get All OBX segments

var obx5 = message.getAllNodes('OBX-5');
 
 
 
//Go over each OBX segement and evaluate what to remove
 
for (var i = 0; i < obx5.length; i++) {
 
   //Check if OBX-5 has HIV in the field
 
   if (StringUtils.containsIgnoreCase(obx5[i], 'HIV')) {
 
      //Remove it if it does have HIV
 
      message.removeNode('OBX[' + (i+1) + ']');
 
   }
 
}
 

2 Answers

+1 vote
 
Best answer

Instance is a reference to the order of segments in a message. The first OBX would be instance one and can be referenced with a node path of OBX[1]. The second OBX is instance 2 or OBX[2]. This is dynamically updated whenever segments are added or removed from the message.

The function “var segs = message.getAllNodes('nodePath') “ will return an array of the segments  in the variable segs, “var segmentCount = message.getCount('nodePath')” will return the number of segments matching the path and store it in the variable segmentCount. Both values are set when the statement is called and are not dynamically updated as segments are added or removed from the message.

Since the instance is dynamically updated and the variables used to control a for loop are static, problems occur when trying to remove segments inside a for loop. This is because as segments are removed the instance is updated but the value stored in the variable is NOT. The variable in the for loop will list one segment as instance 4 when it is now 3 due to the removal of segments.

Here is the recommended solution to remove the segments using a node path that doesn’t include the instance. For example, use a value from the segment and specify it in the node path in the removeNode function.

// Get All OBX segments

var obx5 = message.getAllNodes('OBX-5');

 

//Go over each OBX segement and evaluate what to remove

for (var i = 0; i < obx5.length; i++) {

   //Check if OBX-5 has HIV in the field

   if (StringUtils.containsIgnoreCase(obx5[i], 'HIV')) {

      //Remove it if it does have HIV

      message.removeAllNodes('OBX[@5=' + obx5[i] + ']');

   }

}

answered Apr 23, 2015 by brandon-w-8204 (33,270 points)
selected Apr 29, 2015 by ron-s-6919
0 votes

This can still be done with the instance variable, but in order to work you will need to reverse the for loop and go in reverse order so that segments are removed from the bottom instead of the top.

 

//Get Count of the AL1 segments

var al1Count = message.getCount('AL1');

 

//FOR loop reversed to process last AL1 first

for (var i=al1Count ; i > 0 ; i--) {

   //If statement to check the length ofr AL1-3.2

   if (message.getNode('AL1-3.2', i).length() === 0) {

      message.removeFirstNode('AL1[' + i + ']');

   }

}

 
answered Apr 29, 2015 by ron-s-6919 (4,480 points)
...