web stats
Delete Duplicate IN1 segments based on 2 fields - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 04-27-2012, 10:27 AM
jaywilliams jaywilliams is offline
Mirth Newb
 
Join Date: Jan 2009
Posts: 22
jaywilliams
Question Delete Duplicate IN1 segments based on 2 fields

I have an ADT agent that receives multiple duplicate IN1 segments, but the definition of duplicate deals only with the IN1.3 and IN1.36, so the entire segment doesn't have to be a true duplicate but if these two fields match in more than one IN1 segment, I need to delete both IN1 segments. Some of the ADT messages contain as many as 20 IN1 segments as their account in the EPM application hasn't been cleaned up. The receiving application doesn't care if the segments are still valid or accurate, it just doesn't want any duplicates, based on those 2 fields. Anyone ever encountered that before or have an idea how to tackle this?
Reply With Quote
  #2  
Old 04-28-2012, 06:50 PM
nelson.wells nelson.wells is offline
OBX.1 Kenobi
 
Join Date: Sep 2010
Posts: 26
nelson.wells is on a distinguished road
Default

Let me make sure I understand the problem. If there exists two IN1 segments in a message whose IN1.3 and IN1.36 are the same across the segments, then delete both the segments?

There are a couple of ways to approach this problem. The brute force way is to loop through every IN1 segment (n) and then, in that loop, loop through every IN1 segment again (o). Compare the values of IN1.3 and IN1.36 in the n iteration to the o iteration, and if they match, delete both segments. You'll need a couple of counters to keep track of which segments you need to delete. Since this requires a nested loop, it will be slow.

Another way is to set up a data structure to keep track of the values of a given iteration. You'll loop through each IN1 segment, and if you've encountered that value before (based on what is in your data structure) delete the current segment and also the previous segment. With this solution, you only need one loop. Execution speed will be faster but it will also take up a little more memory.

If you need more help, let me know and I can probably whip up some code (can't guarantee any kind of quick delivery, though ).
Reply With Quote
  #3  
Old 04-30-2012, 02:25 PM
jaywilliams jaywilliams is offline
Mirth Newb
 
Join Date: Jan 2009
Posts: 22
jaywilliams
Default

If you could on the second option, that would be great. I am not sure I fully understand what you mean by setting up a data struction. To me, that is setting up a db, which from the sound of your response, seems to not be exactly what you are referring to.

I think I may have figured out a way for the first option, but I agree that it would be slow and extremely inneficient, by looping inside loops. So far, I haven't had any luck with it, but then again, I haven't had much of a chance to code and debug yet.

Thanks for any help you can provide.
Reply With Quote
  #4  
Old 04-30-2012, 05:05 PM
glenn71 glenn71 is offline
OBX.2 Kenobi
 
Join Date: Nov 2010
Location: Sydney Australia
Posts: 87
glenn71 is on a distinguished road
Default

Would this be more efficient and easier to code? For each IN1 segment you could insert IN1.3 and IN1.36 into a hashset that does not allow duplicates. Then re-create your IN1 segments from the hashset at the end.
Reply With Quote
  #5  
Old 04-30-2012, 06:06 PM
glenn71 glenn71 is offline
OBX.2 Kenobi
 
Join Date: Nov 2010
Location: Sydney Australia
Posts: 87
glenn71 is on a distinguished road
Default

On second thoughts this should do what you want..

Code:
Array.prototype.has = function(value) {
	for (var i = 0; i < this.length; i++)
		if (this[i] === value)
			return true;
	return false;
}


var arr = new Array();
var j=0;

for each (in1 in msg.IN1)
{
      var txt = in1['IN1.3']['IN1.3.1'].toString() + in1['IN1.36']['IN1.36.1'].toString();
      if (arr.has(txt) {
             delete msg['IN1'][j]
             j--;
      } else {
             arr.push(txt);
      }
      j++;
}

Last edited by glenn71; 05-02-2012 at 11:12 PM.
Reply With Quote
  #6  
Old 05-01-2012, 07:52 AM
jaywilliams jaywilliams is offline
Mirth Newb
 
Join Date: Jan 2009
Posts: 22
jaywilliams
Default

Mirth is complaining about an error on line 7: if (this i === value)

The Mirth error is: Error on Line 7: missing ) after condion. I have tried adding additional parenthesis, which it doesn't appear to need, but it still complains. Any ideas what the issue is? Thanks for the code, BTW. This kind of programming is still over my head.
Reply With Quote
  #7  
Old 05-01-2012, 08:03 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 4,072
narupley is on a distinguished road
Default

Quote:
Originally Posted by jaywilliams View Post
Mirth is complaining about an error on line 7: if (this i === value)

The Mirth error is: Error on Line 7: missing ) after condion. I have tried adding additional parenthesis, which it doesn't appear to need, but it still complains. Any ideas what the issue is? Thanks for the code, BTW. This kind of programming is still over my head.
it was probably a typo; just replace that function with:

Code:
Array.prototype.has = function(value) {
	for (var i = 0; i < this.length; i++)
		if (this[i] === value)
			return true;
	return false;
}
Reply With Quote
  #8  
Old 05-01-2012, 10:29 AM
jaywilliams jaywilliams is offline
Mirth Newb
 
Join Date: Jan 2009
Posts: 22
jaywilliams
Default

The code works sort of. Thanks for everyone's participation. This code will remove the second duplicate IN1 segment, but it doesn't remove both that are duplicate. I need for the code to remove both segments if they are duplicate to each other. Can this be done with any adjustment to this code? Thanks.
Reply With Quote
  #9  
Old 05-07-2012, 01:42 PM
jaywilliams jaywilliams is offline
Mirth Newb
 
Join Date: Jan 2009
Posts: 22
jaywilliams
Default

I finally worked with this and found a way to modify it to delete all insurances that were duplicates and not just teh subsequent ones. The code may be a bit clunky, but it works. I have it set to only run on those messages where I have more than one IN1 segment so that it doesn't run every time. Consequently, I didn't need the return false statement. Here it is:

// this step verifies that it will only run if there are 2 or more insurances
var IN1count = msg['IN1'].length();

if (IN1count != '0' && IN1count != '1')
{
// This process loops through and addes all new insurances IDs to the array and any duplicates
// it finds it loops back through the insurances and deletes all duplicates before moving on to
// the next insurance.
Array.prototype.has = function(value) {
for (var i = 0; i < this.length; i++)
if (this[i] === value)
return true;
}


var arr = new Array();
var j=0;


for each (in1 in msg.IN1)
{
var txt = in1['IN1.3']['IN1.3.1'].toString() + in1['IN1.36']['IN1.36.1'].toString();
var DelMatchID = "";
var DelMatchPol = "";

if (arr.has(txt)) {

DelMatchID = in1['IN1.3']['IN1.3.1'].toString();
DelMatchPol = in1['IN1.36']['IN1.36.1'].toString();

for (var k=0;k<msg['IN1'].length();k++)
{
var toMatchID = msg['IN1'][k]['IN1.3']['IN1.3.1'].toString();
var toMatchPol = msg['IN1'][k]['IN1.36']['IN1.36.1'].toString();
if (toMatchID == DelMatchID && toMatchPol == DelMatchPol)
{
delete msg['IN1'][k];

k--;
}
}
}
else {
arr.push(txt);
}
j++;
}

}
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 -7. The time now is 01:35 AM.


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