web stats
Search for alpha characters and delete OBX - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-13-2019, 09:19 AM
Mits87 Mits87 is offline
OBX.2 Kenobi
 
Join Date: Nov 2016
Posts: 58
Mits87 is on a distinguished road
Default Search for alpha characters and delete OBX

I need to check if OBX 5.1 contains any alpha characters and if it does then delete the entire OBX segment. I have below code in place but somehow it's not deleting the OBX line. There is no error either.

Code:
for ( var a=0; a < msg['OBX'].length(); a++ )
{
	if (msg['OBX'][a]['OBX.5']['OBX.5.1'].toString().indexOf(/^[a-zA-Z]*/) > -1 )
	{
		 logger.info(msg['OBX'][a]['OBX.5']['OBX.5.1'].toString());
		 delete msg['OBX'][a];
		 var OBXCounter = 1;
			for each (seg in msg..OBX)
			{
  			seg['OBX.1']['OBX.1.1'] = OBXCounter;
  			OBXCounter++;
			}
	} 
}
Here is the sample message I use

MSH|^~\&|||||20180718104950||ORU^R01|9705201905130 11103|P|2.4|||AL|AL
PID|1||10000000
ORC|NW|||||||||||||||||
OBR|1|||Test1|||20180622115219||||O||||VEN||
OBX|1|ST|Glu||>180|mmol/L||H|||F|||||2||
OBX|2|ST|WBC||57|mg/dL|||||F|||||2||
OBX|3|ST|UREA||<23|mg/dL|||||F|||||2||
OBX|4|ST|BE||NaN|mg/dL|||||F|||||2||
OBX|5|ST|Glu||HIGH|||||||||||2||
OBX|6|ST|Site||ED|||||||||||2||
OBX|7|ST|Verified||No|||||||||||2||
Reply With Quote
  #2  
Old 05-13-2019, 10:24 AM
Brad_AZ Brad_AZ is offline
Mirth Newb
 
Join Date: Mar 2018
Location: Arizona, USA
Posts: 17
Brad_AZ is on a distinguished road
Default

When I test it, this returns false every time.

(msg['OBX'][a]['OBX.5']['OBX.5.1'].toString().indexOf(/^[a-zA-Z]*/) > -1 )

So I'm looking at that....
__________________
SIG|1|Mirth Certified Interface Analyst^Cancer Treatment Centers of America|
Reply With Quote
  #3  
Old 05-13-2019, 12:26 PM
Brad_AZ Brad_AZ is offline
Mirth Newb
 
Join Date: Mar 2018
Location: Arizona, USA
Posts: 17
Brad_AZ is on a distinguished road
Default

I couldn't get anything from your expression other than -1 so I went in a different direction. This seems to work for me with your message and others that I mocked up.

Code:
var setID = 1;                                                             // sets intial value for SET ID

for ( var a=0; a < msg['OBX'].length(); a++ )
{

     var alpha = /[a-z]/;                                                  // defines a-z as values in expression
	var str = msg['OBX'][a]['OBX.5']['OBX.5.1'].toString().toLowerCase(); // converts alpha to lowercase
	if (str.match(alpha)) {                                               // looks for a-z in string
	
		 delete msg['OBX'][a];
		 a = a-1;                                                        // resets the counter back one because you deleted the segment
	}
	
else {
msg['OBX'][a]['OBX.1']['OBX.1.1'] = setID;                                // populates SET ID starting with 1
setID ++                                                                  // increments set ID
}
}
__________________
SIG|1|Mirth Certified Interface Analyst^Cancer Treatment Centers of America|

Last edited by Brad_AZ; 05-13-2019 at 12:29 PM.
Reply With Quote
  #4  
Old 05-13-2019, 12:26 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 902
agermano is on a distinguished road
Default

Brad is on the right track. String.prototype.indexOf takes a string parameter, not a regex. I think you want to use String.prototype.search instead.

Your regex matches a string that starts with 0 or more alpha chars. I don't think that's what you want, as search will return index 0 for any string. You can use the i flag to make it case insensitive.

You probably don't want to nest your loops. Wait until you're finished deleting to renumber them instead of renumbering on every delete.

If you are deleting using an index, you need to iterate in reverse and delete from the back first because it will change the index of the segments following the one you delete. I.e. if you have 5 segments and want to delete the second and third, if you delete the second first, everything after it shifts down, and you will end up skipping the 3rd and deleting the 4th instead.

Code:
// delete with a for each
for each (var obr in msg.OBX) {
    if (obx['OBX.5']['OBX.5.1'].toString().search(/[A-Z]/i) !== -1) {
        delete obx[0];
    }
}

// OR delete iterating backwards
for (var i = msg.OBX.length() - 1; i >= 0; i--) {
    if (msg['OBX'][i]['OBX.5']['OBX.5.1'].toString().search(/[A-Z]/i) !== -1) {
        delete msg['OBX'][i];
    }
}

// renumber after all of your deletes are finished
for (var i = 0; i < msg.OBX.length(); i++) {
    msg['OBX'][i]['OBX.1']['OBX.1.1'] = i + 1;
}
Reply With Quote
  #5  
Old 05-13-2019, 12:34 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 902
agermano is on a distinguished road
Default

We posted at the same time Brad's second post addresses all of the issues I pointed out in a different way, but it still works.

match is probably more appropriate than search in this case. I wanted to show the indexOf equivalent that could take a regex.

Decrementing the loop index variable whenever you delete takes care of the issue where you would skip values when iterating forward and allows you to renumber the setID in the same loop without nesting.
Reply With Quote
  #6  
Old 05-15-2019, 12:30 AM
Mits87 Mits87 is offline
OBX.2 Kenobi
 
Join Date: Nov 2016
Posts: 58
Mits87 is on a distinguished road
Default

You guys are really awesome as always. Thank you Agermano and Brad. This was the first time I used the interation loop with regular expression so the explanation really helps.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 03:11 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation