View previous topic :: View next topic
|
Author |
Message |
satyapn9
New User
Joined: 08 Oct 2010 Posts: 27 Location: India
|
|
|
|
Hi, I have an issue in finding duplicates issue in a sorted array. Below are the details.
There is a sorted array and TABLE-FIELD-YY has a default value of 'N' and I am setting to 'Y' if a value is found for TABLE-FIELD-XX. It was implemented using a binary search ( SEARCH ALL ). The value 'Y' will not set to duplicates of TABLE-FIELD-XX as it is doing a binary search.
Here I would like to make use of below loops to move 'Y' for all duplicates of TABLE-FIELD-XX. But it is not moving 'Y' to all duplicates eventhough below logic finding all duplicates in that array. I mean dups-count is good.
Code: |
PERFORM VARYING I FROM 1 BY 1
UNTIL I > MAX-COUNT-ARRAY
COMPUTE J = I + 1
PERFORM VARYING K FROM J BY 1
UNTIL K > MAX-COUNT-ARRAY
IF TABLE-FIELD-XX(I) =
TABLE-FIELD-XX(K)
MOVE 'Y' TO TABLE-FIELD-YY(K)
ADD 1 TO DUPS-COUNT
END-IF
END-PERFORM
END-PERFORM. |
Can some one help me to achieve this? Am i missing any thing here?? |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
To reconfirm my understanding
You want to move 'Y' to TABLE-FIELD-YY(I) when TABLE-FIELD-XX(I) is a duplicate value? |
|
Back to top |
|
|
satyapn9
New User
Joined: 08 Oct 2010 Posts: 27 Location: India
|
|
|
|
Yes, I would like to move 'Y' for all duplicates of TABLE-FIELD-YY(I) |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Try this untested code
Code: |
PERFORM VARYING I FROM 1 BY 1
UNTIL I > MAX-COUNT-ARRAY
IF I = 1
MOVE TABLE-FIELD-XX(I) TO TEMP
ELSE
IF TABLE-FIELD-XX(I) = TEMP
MOVE 'Y' TO TABLE-FIELD-YY(I)
END-IF
END-IF
MOVE TABLE-FIELD-XX(I) TO TEMP
END-PERFORM |
|
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
How do you know "it is not moving Y"?
Why would you think "dups-count" is "good"?
A
A
A
This would give a count of 3 (which is one original and two duplicates),
B
B
B
B
This would give a count of 6,
C
C
This would give a count of 1.
If you are doing the SEARCH ALL to set something to Y, why are you then just blindly setting all duplicates to Y afterwards?
If you can explain the whole thing, we can get you out of the tangle. |
|
Back to top |
|
|
satyapn9
New User
Joined: 08 Oct 2010 Posts: 27 Location: India
|
|
|
|
Hi Bill,
I am searching for a TABLE-FIELD-XX(I) in the array using SEARCH ALL and would like to set a FLAG i.e. 'Y' to TABLE-FIELD-YY(I) if a search is success.
Here I wanted to move 'Y' to all its duplicates in that array. But I do not know to how to achieve this using SEARCH ALL fuction itself.
So would like to make use of another looping condition to fulfill this.
Please let me know if I am not clear. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
It is not so good to have duplicates in a table when using SEARCH ALL (emphasis added).
Language Reference wrote: |
The results of a SEARCH ALL operation are predictable only when:
The data in the table is ordered in ASCENDING KEY or DESCENDING KEY
order
The contents of the ASCENDING or DESCENDING keys specified in the WHEN
clause provide a unique table reference. |
Unlike when the table is not in order (which plain won't work), yours "works" but which individual record out of any set of duplicates is found cannot be known without knowing the results of that exact table structure.
You might get
Code: |
x Y
x
x
or
x
x Y
x
or
x
x
x Y
|
Depending on the location of the group of data represented by "x".
You use SEARCH ALL to find an item of data (usually a "look-up" type task).
Now, it depends what you have got, which you haven't told us, as to how best to do your task.
Perhaps, a table of the unique keys, with an "index" which points to the group of full data in a separate table. When you find the match, use the index as a starting point to update all the data in your separate table which match that key.
As I said earlier, if you can be specific about what your data is and what you are trying to do, then you'll get a better answer. |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Quote: |
Try this untested code
Code:
PERFORM VARYING I FROM 1 BY 1
UNTIL I > MAX-COUNT-ARRAY
IF I = 1
MOVE TABLE-FIELD-XX(I) TO TEMP
ELSE
IF TABLE-FIELD-XX(I) = TEMP
MOVE 'Y' TO TABLE-FIELD-YY(I)
END-IF
END-IF
MOVE TABLE-FIELD-XX(I) TO TEMP
END-PERFORM |
|
|
Back to top |
|
|
satyapn9
New User
Joined: 08 Oct 2010 Posts: 27 Location: India
|
|
|
|
Yes, Actually my requirement is to do a look-up of a table(array) using a key from a FILE. This table data is stored with ASCENDING order of that key (TABLE-FIELD-XX).
I should write up the rows to output which are in table but NOT in FILE based on that key. To achieve this, I have added a new field TABLE-FIELD-YY to the table
and while loading the table i have moved 'N' to TABLE-FIELD-YY for all rows.
Now while performing a look-up, if a match found then I will set 'Y' to TABLE-FIELD-YY for the corresponding entry of TABLE-FIELD-XX.
Am I clear on this? |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
If your file is in "key order" you can consider a "matched file" process.
If the purpose of the use of the file is just for this task, you can extract and sort the key anyway.
You mention "table". Is your data coming from DB2? You could extract a "flat" file from DB2 and do the match.
If you do the above, the whole thing can be done very simply with SORT and JOINKEYS (if your SORT is up-to-date).
You could have, as I suggested earlier
OCCURS containing all your data
Extract from that the unique "key" and the index/subscript of that value in this OCCURS, to a new OCCURS.
Use the new OCCURS to establish the match of the key, then, using the index/subscript update in your main table all records of that key.
Pandora-Box,
The "Y" is supposed to be only for those keys which already have a "Y" "somewhere" in the records which have the same key. |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
The requirement change my suggesion will never help here |
|
Back to top |
|
|
satyapn9
New User
Joined: 08 Oct 2010 Posts: 27 Location: India
|
|
|
|
Thanks for your suggestion Bill,
But I cannot change the requirement as it is designed in such a way.
I mentioned TABLE here is ARRAY only. It has several fields in it and already populated with data and I should do look-up over it and should write up records to output if key exists in array but not in FILE.
Only thing I am looking a help here is to find those DUPLICATES and set up a flag. |
|
Back to top |
|
|
satyapn9
New User
Joined: 08 Oct 2010 Posts: 27 Location: India
|
|
|
|
Pandora, Your suggested code is tested by me but not successful in this criteria. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
satyapn9,
You have your existing table. You keep that.
From your existing table, you extract your "key" field, and the index/subscript for that entry - and only do this for the first key value in the table (ie, all the keys will be unique).
You use the new table for the SEARCH ALL.
When you get a match, you go back to your original table, with the index/subscript for the match, and you update all the entries in your original table which have the same key as you just got the match for.
If you are prevented from doing that, then you have to kick it back to the person who is insisting it is done in the other way, and ask them for some stupid, unclear, hard-to-maintain code to do what they so cleverly want. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
satyapn9 wrote: |
Pandora, Your suggested code is tested by me but not successful in this criteria. |
Because you were unclear about what you were trying to achieve. |
|
Back to top |
|
|
satyapn9
New User
Joined: 08 Oct 2010 Posts: 27 Location: India
|
|
|
|
Hi Bill, You didn't understand my question well that why my logic is not working correctly. Instead you suggesting the requirement changes. But it is not possible currently.
Anyways thank you for your time. |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Quote: |
You didn't understand my question well that why my logic is not working correctly. |
Well Dear Satyapn you hardly explained things to us for providing you a fruitful answer |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
satyapn9:
Your first problem is that you do not understand COBOL Attempting to implement SEARCH ALL on a table with duplicate key values is guaranteed to fail -- period. Your statement that you used SEARCH ALL at first is a sign you should be in Beginners and Students Forum instead of this forum -- no matter how many years of "experience" you claim.
Your second problem is that you have yet to adequately explain precisely what your problem is, yet you keep complaining that suggested solutions don't work.
The logic you originally posted will work fine -- AS LONG AS THE KEYS ACTUALLY MATCH. I suspect what you have is key values that have non-display data in them, in which case two keys may appear to match but do not actually match.
Recommendation: use DISPLAY on some key values that you think should match and review the program output using hexadecimal display. If the key values that you think should match do not actually match, byte for byte, in hexadecimal then your logic is working correctly and your data is the problem. In any case, we will need to see some of these DISPLAY output statements before we can make other suggestions. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
satyapn9 wrote: |
Hi Bill, You didn't understand my question well that why my logic is not working correctly. Instead you suggesting the requirement changes. But it is not possible currently.
Anyways thank you for your time. |
There is no business requirement to "patch something up" to do it in a bad way.
Nothing I have suggested can possibly change the business requirement.
As I said. If you are just a coder, and you have to code what you are told to, go back to the person who told you, and get them to produce the ugly, high-maintenance, waste of resources solution that you seem to regard as your "requirement". |
|
Back to top |
|
|
|