View previous topic :: View next topic
|
Author |
Message |
sasikumar1984
Active User
Joined: 02 Jul 2007 Posts: 109 Location: Chennai - India
|
|
|
|
Hi,
I have an array with below structure
01 WRK-CLOTHES OCCURS 10 TIMES
05 WRK-CLOTH PIC X(4)
05 WRK-VALUE PIC S9(3)V9(2)
I have filled the array already with values from database, now i want sort above array and fill the data in the below array
01 YRK-CLOTHES OCCURS 10 TIMES
05 YRK-CLOTH PIC X(4)
05 YRK-VALUE PIC S9(3)V9(2)
the different type of WRK-CLOTH are 'A001' 'A002' 'C001' 'C002' and few other values that start with 'B' or 'D'. Now i want sort the array such a way that first it should fill A001 and A002 and then that starts with 'B' or 'D' by highest WRK-VALUE to lowest WRK-VALUE and then C001 and C002. i may or may not gey A001, A002, C001, C002. Some timesi get one of this or two of this. its like depends. But the if it there then it should follow the order for SORTING.
Here is the eg:
A001 01020
B001 01030
B003 10400
B005 60500
C001 00920
My sorted array shoul be like this
A001 01020
B005 60500
B003 10400
B001 01030
C001 00920
Can someone provide me sample code for this. so that i will develop as per my values.
Thanks in advance,
Anand |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
You need to post a more complete set of example data. SHow multiple A, C, and D entries in both the input and output.
An easy thing for you to do would be to use an SD and an internal SORT to arrange the data in the order you want. |
|
Back to top |
|
|
sasikumar1984
Active User
Joined: 02 Jul 2007 Posts: 109 Location: Chennai - India
|
|
|
|
i gave simple example which solves my problem also. what is SD? i dont understand that. And internal SORT?? can someone provide me some sample code?? |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
If you have an A001 and A002, what order to you expect them in the output? Ditto for C001 and C002? You did not provide any explanation in your sample as to what to do in these cases.
There's a link at the top of the page to manuals -- the COBOL Language Reference and Programming Guide have details about using the SORT verb in COBOL to do an internal SORT using the SD file definition clause, which is used instead of FD for sorts. You can also find out about INPUT PROCEDURE and OUTPUT PROCEDURE which gives even more control over the sort. |
|
Back to top |
|
|
sasikumar1984
Active User
Joined: 02 Jul 2007 Posts: 109 Location: Chennai - India
|
|
|
|
if its A001 or A002, then it can be filled immediatly, no need to check the balances, but begingin of the array. Like that C001 and C002, but at end of the array. only for 'B' or 'D' series we need check value variable and fill from highest to lowest inbetween A and C series. If there is no A series means then it will start with E series and C series always end. And if there is not A and C series means then only E series with sorted order. hope i am clear here. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
What you are saying is not clear to me. Are you stating that there will only be one A record? If this is not what you are saying, do you care if the order is A002 A001 ... versus A001 A002? The same applies for the C records -- is there only ever one? If there is more than one, do you care which order they come out in? |
|
Back to top |
|
|
sasikumar1984
Active User
Joined: 02 Jul 2007 Posts: 109 Location: Chennai - India
|
|
|
|
Hai Robert,
here is the example...
First array
A001 01020
C001 00920
C002 00890
B001 01030
B003 10400
B005 60500
C001 00920
My SORTED array should be
A001 01020
B005 60500
B003 10400
B001 01030
C001 00920
C002 00890
We are not sure i always receieve A001 and A002. sometime i receieve both and sometimes only one and sometimes none from A series. Like that C series also. E series should be sorted based on VALUES and should be inbwteen A and C series. |
|
Back to top |
|
|
sasikumar1984
Active User
Joined: 02 Jul 2007 Posts: 109 Location: Chennai - India
|
|
|
|
Hai Robert,
here is the example...
First array
A001 01020
C001 00920
C002 00890
B001 01030
B003 10400
B005 60500
My SORTED array should be
A001 01020
B005 60500
B003 10400
B001 01030
C001 00920
C002 00890
We are not sure i always receieve A001 and A002. sometime i receieve both and sometimes only one and sometimes none from A series. Like that C series also. E series should be sorted based on VALUES and should be inbwteen A and C series. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
i gave simple example which solves my problem also. |
No, it actually doesn't. . . It is incomplete. When you implement code, the code will not know what you know and must be written for any and all things mentioned in "the rules".
Quote: |
What you are saying is not clear to me |
Mee too. . . What you are saying is only to clear to you. Keep in mind that while your situation/requirement is completely clear to you, it possibly is not to everyone who reads it.
Quote: |
And internal SORT?? |
As suggested - use the "IBM Manuals" link at the top of the page and select the Language Reference for the version of cobol your system uses. As Robert mentioned, you want INPUT PROCEDURE/OUTPUT PROCEDURE processing. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Until you put this up
Quote: |
First array
A001 01020
C001 00920
C002 00890
B001 01030
B003 10400
B005 60500
My SORTED array should be
A001 01020
B005 60500
B003 10400
B001 01030
C001 00920
C002 00890 |
The simplest solution to your requirement was to just do
Code: |
SORT FIELDS=(1,1,CH,A,2,4,CH,D) |
but that won't work with what you've posted.
You need to create an extra field on your record (through COBOL for example) where if the first character is B or D move the next three bytes to the extra field, otherwise move 999 - the next three bytes to the extra field. Then sort on 1,1,CH,A and the extra field descending. That will put the data in the sequence your last post indicated. Once the sort is done you do not need the extra field. |
|
Back to top |
|
|
sasikumar1984
Active User
Joined: 02 Jul 2007 Posts: 109 Location: Chennai - India
|
|
|
|
I have to do it in COBOL. Not in JCL SORTCARD. in comes in mid of the program. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Not a big deal -- the sort SD can be any layout you want. Define the SD 01 as
Code: |
01 SD-RECORD.
05 SD-EXTRA-FIELD PIC 9(03).
05 SD-DATA-RECORD.
10 SD-FIRST-CHAR PIC X(01).
10 FILLER PIC X(...).
|
Code your sort statement in the PROCEDURE DIVISION as
Code: |
SORT SORT-FILE
ASCENDING KEY SD-FIRST-CHAR
DESCENDING KEY SD-EXTRA-FIELD |
and use INPUT PROCEDURE to set the extra field as I discussed earlier. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hi Robert,
I believe the sort needs 3 sort keys (one for the first byte, one for those that are ascending and another for descending) and the code needs to use one or the other of the a/d sort keys depending on the first byte.
If i've not misunderstood something. . . |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Dick: if the first character of the record is B or D, I recommended in my earlier post moving the value to the sort extra field, otherwise setting the sort extra field to 999 minus the value. That way, using my suggested sort, the record is sorted on the first byte, then ascending on the next three bytes if the first byte is A, C, E but descending for B and D. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Yup, saw that earlier, but didn't see it in the code snippet.
Then i got to thinking (probably not a good thing ) that having an asecnding field and a descending field might be easier for some to handle than inverting the number. . .
Maybe we'll hear back on what progress has been made. . .
d |
|
Back to top |
|
|
|