Sidebar

When would it be appropriate to use a reverse for loop?

0 votes
8.6K views
asked Oct 5, 2015 by gary-t-8719 (14,860 points)

The following article will help the user visualize how a reverse for loop can help with removing one or more HL7 segments from a message.

The following HL7 message will be used as an example. In this example we will be removing TD Booster OBX segments (OBX segments number 2 and 5.) as identified by the CPT-90702 code in OBX-3.

MSH|^~\&|LinkLogic-Training|TEST000^E IM|Death|E IM|20150422142426||ORU^R01|1745331796181550|P|2.3.1|||NE|NE
PID|1||677|MR-000-002|Bassett^Don^C.||19550424|M||2106-3|12155 SW Broadway^^Beaverton^IL^60005^USA||(503) 629-5541^^PH^dbassett@aol.com~(503) 692-8956^^FX|(503) 692-8955|English|M|||543-34-5621|||N
PV1|1|O|^^^E IM||||kstarr|^Misiriliogu^Youseff
OBR|1|||1^CPK|||20080209180000|||||||||kstarr||LR
OBX|1|ST|LOI-2157-6^CPK||6|U/L|0-250||||F|||20091006180000
OBX|2|ST|CPT-90702B^TD BOOSTERBY||TGrey||||||R|||20091030101700
OBX|3|ST|LOI-1783-0^ALK PHOS||88|U/L|35-100||||F|||20091007093000
OBX|4|ST|CPT-90702L^TD BOOSTERLO||37921||||||R|||20091030101700
OBX|5|ST|LOI-2339-0^BG RANDOM||86|mg/dL|70-125||||F|||20091007093000
 

 

1 Answer

0 votes
 
Best answer

To begin here is the code that will perform the requested task. 

1.   var obxSegments = message.getAllNodes('OBX');
2.   for (var i = (obxSegments.length -1); i > -1; i--) {
3.      var obxSegment = qie.parseHL7String(obxSegments[i]);
4.    
5.      if (StringUtils.startsWith(obxSegment.getNode('OBX-3.1'), 'CPT-90702')) {
6.         var obx3 = obxSeg.getNode('OBX-3');
7.           message.removeAllNodes('OBX[@3='+ obx3 +']');
8.      }
9.   }
 
We will now break down the code and explain how it works.
1. To begin the first line of code returns an Array or list of OBX segments. This list or the variable obxSegments is static in that once we get the list it does not change as we progress through the loop.
 
OBX Array
 
2. Becuase the list is generated before we enter the for loop the length of the Array will reflect the same number of segments as found in the source message. So if there are 5 OBX segments as in our example message then the length of obxSegments as seen on line 2 will be 4. This is because an Array is zero based (0,1,2,3,4) which accounts for our five OBX segments. This means we will go through the loop 5 times thus processing each OBX segment to determine if it needs to be removed.
 
3. Line 7 will be removing OBX segments from our messge object or variable each time we run through the loop. So after we remove the first OBX segmet we will have one less OBX segment then our original message. So it is important to see the differnece between the static number of OBX segments or the i variable counter and the dynamic number of OBX segments in our message.
 
4. To see this difference and the effect of the for loop copy just the OBX segments from the the sample message into a excel spreadsheet with one segment in each row.
 
 
 
5. In a traditional "forward" for loop line 6 as shown below would be removing OBX segments from a the top down. This would mean that the first OBX segment to be removed woud be row 2 in the excel spreadsheet.  A forward for loop would be written like this:
 
1.   var obxSegments = message.getAllNodes('OBX');
2.   for (var i = 1; i < obxSegments.length; i++) {
3.      var obxSegment = qie.parseHL7String(obxSegments[i]);
4.    
5.      if (StringUtils.startsWith(obxSegment.getNode('OBX-3.1'), 'CPT-90702')) {
6.            message.removeAllNodes('OBX[' + (i+1) + ']');
7.      }
8.   }
 
6. To see the first time trough the loop highlight the first OBX segment (row 1) in the excel spreadsheet to mimic the first time through the loop. The i counter at this point would be equal to "1". The if statement on line 5 of the code would equate to false as this does not match a segment we want to remove.
 
7. To see the second time thorugh the loop highlight the second OBX segment (row 2) in the excel spreadsheet to mimic the second time through the loop. Keep in mind that the i variable is now equal to "2". This time the if statement on line 5 of the code equates to true and therefore removes this OBX segment from the message. So if you now manually delete row 2 from the excel spreadsheet you will now see it removed and notice that all the lines below it now move up one row. Thus row 3 now becomes row 2 and row 4 become row 3 and row 5 became row 4.
 
 
8. The third time through the for loop the i variable now equates to 3 and row 2 which now holds the data from row 3 the previouse time through the loop will be skiped. 
 
9. Now repeat the same exercise in the excel spreadsheet by restoring the row we deleted and this time by using a "reverse" for loop we will now evaluate and remove segments from the bottom up. So this time the first OBX segment we will remove will be row 4 in the spreadsheet which would be the second time through the loop. If you manually delete row 4 you will still see that row 5 moves up into row 4 but that is ok since we have already evaluated row 5 the first time through the loop. We can now continue to process the OBX segments above row 4 without skipping an OBX segment.
 
 
 
 
answered Oct 5, 2015 by gary-t-8719 (14,860 points)
selected Apr 16, 2020 by amanda-w-3695
...