Get index of different characters while comparing two string



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Get index of different characters while comparing two string

Postby vivek88 » Tue Sep 17, 2013 6:26 pm

Let's say there are two strings - PR-ACT-SOURCE-DETAIL-1 and PR-ACT-SOURCE-DETAIL-2. i want to compare these two string and find out the position where the difference is found.

I tried to handle the scenario something like this way -

 PERFORM VARYING N FROM 1 BY 1 UNTIL N > 5000                                                                 
    IF PR-ACT-SOURCE-DETAIL-1 OF TRANSACTION-RECORD-1(N:1)   
        IS NOT EQUAL TO                                   
       PR-ACT-SOURCE-DETAIL-2 OF TRANSACTION-RECORD-2(N:1) 

        MOVE 'Y' TO WS-DIFF-FOUND   
        DISPLAY 'DIFFERENCE FOUND AT POSITION' N
    END-IF
 END-PERFORM



Problem with the above code is that perform loop occurs 5000 times and if I need to compare such 10,000 strings so the execution time becomes too high.

Is there any other way to do the same thing which will require lesser execution time?
vivek88
 
Posts: 2
Joined: Thu Aug 29, 2013 1:51 am
Has thanked: 1 time
Been thanked: 0 time

Re: Get index of different characters while comparing two string

 

Re: Get index of different characters while comparing two st

Postby Robert Sample » Tue Sep 17, 2013 7:11 pm

1. You're talking about 50,000,000 comparisons -- so it will take some time, no matter what.
2. You search all 5000 characters regardless -- why not change your code to stop the comparison loop when you find a difference, unless you need to know all such differences?
3. You might get better results by comparing the two variables and only if they are not equal perform the loop.

These users thanked the author Robert Sample for the post:
vivek88 (Tue Sep 17, 2013 7:19 pm)
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times

Re: Get index of different characters while comparing two st

Postby vivek88 » Tue Sep 17, 2013 7:18 pm

Actually i can not stop the comparison loop when a difference found because I have to find all position number of string(which is lumps of data) when a difference is found.
vivek88
 
Posts: 2
Joined: Thu Aug 29, 2013 1:51 am
Has thanked: 1 time
Been thanked: 0 time

Re: Get index of different characters while comparing two st

Postby Robert Sample » Tue Sep 17, 2013 7:39 pm

In that case, there is not much you're going to be able to do to speed up the process -- you have to do 50,000,000 comparisons. If a lot of the variables match, coding a check for them to be equal before starting the loop would take some of the comparisons out of the process; otherwise, plan on it taking a bit of time.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times

Re: Get index of different characters while comparing two st

Postby dick scherrer » Tue Sep 17, 2013 8:26 pm

Hello and welcome to the forum,

3. You might get better results by comparing the two variables and only if they are not equal perform the loop.
If you do this, you may avoid much of the overhead. Unless nearly every record will have 1 or more mis-matches.

If you only do the loop for 1 or 2 % of the records (the ones with adifference), the overhead will be drastically less than doing the whole loop for every record. If a high % of the data has mis-matches, the volume will prevent people from using difference info.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: Get index of different characters while comparing two st

Postby BillyBoyo » Tue Sep 17, 2013 8:41 pm

Don't post in multiple places at the same time please. Stackoverflow for this one.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Get index of different characters while comparing two st

Postby chaat » Wed Sep 18, 2013 2:33 am

is this on Enterprise COBOL ? if so is your subscript defined as PIC S9(8) COMP ?

that can make is pretty large difference if you had it as PIC 9(4) ===> usage display subscripts perform probably 5 times worse than binary subscripts

also the compiler option TRUNC(OPT) would help as well.
chaat
 
Posts: 14
Joined: Sun Aug 16, 2009 11:07 pm
Location: St. Cloud, Minnesota
Has thanked: 0 time
Been thanked: 1 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post