Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   Search for alpha characters and delete OBX (http://www.mirthcorp.com/community/forums/showthread.php?t=219141)

Mits87 05-13-2019 09:19 AM

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.


for ( var a=0; a < msg['OBX'].length(); a++ )
        if (msg['OBX'][a]['OBX.5']['OBX.5.1'].toString().indexOf(/^[a-zA-Z]*/) > -1 )
                delete msg['OBX'][a];
                var OBXCounter = 1;
                        for each (seg in msg..OBX)
                          seg['OBX.1']['OBX.1.1'] = OBXCounter;

Here is the sample message I use

MSH|^~\&|||||20180718104950||ORU^R01|9705201905130 11103|P|2.4|||AL|AL

Brad_AZ 05-13-2019 10:24 AM

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....

Brad_AZ 05-13-2019 12:26 PM

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.


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

agermano 05-13-2019 12:26 PM

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.


// 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;

agermano 05-13-2019 12:34 PM

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.

Mits87 05-15-2019 12:30 AM

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.

All times are GMT -8. The time now is 02:39 PM.

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