View previous topic :: View next topic
|
Author |
Message |
baljinders
New User
Joined: 21 Aug 2006 Posts: 72
|
|
|
|
Hi,
Can anyone help me with the logic for getting the below output. The input file is a sequential file . Filed 1 and 2 are the key. The 3rd field in the output file is the counter for number of duplicate occurances per unique key value.
INPUT
----------
Field1 Field2
AAA 111
AAA 111
AAA 111
BBB 111
BBB 222
CCC 111
CCC 111
OUTOUT
------------
AAA 111 3
BBB 111 1
BBB 222 1
CCC 111 2 |
|
Back to top |
|
|
bipinpeter
Active User
Joined: 18 Jun 2007 Posts: 213 Location: Cochin/Kerala/India
|
|
|
|
hI baljinders,
tRY THE BELOW CODE
Code: |
SORT FIELDS=(1,7,CH,A)
INREC FIELDS=(1,7,8:C'001')
SUM FIELDS=(8,3,ZD)
|
|
|
Back to top |
|
|
baljinders
New User
Joined: 21 Aug 2006 Posts: 72
|
|
|
|
Thanks Bipin,
But i want to do it through a cobol program...Also field 1 and Field2 are not contiguos i.e there are some other fields in between them...i should have mentioned it earlier... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
You need to post what you have done so far and explain where your are stuck.
We will help you work on your code but will not write it for you (if the complete code is posted, it will be deleted).
If your data is in sequence you need to read the file accumulating the count for each "key". Each time a new key is detected, print the previous key and the accumulated count. |
|
Back to top |
|
|
baljinders
New User
Joined: 21 Aug 2006 Posts: 72
|
|
|
|
The approach is something like this.. i am not sure it its correct.
Read file
at end
move y to eof
not at end
add 1 to rec-cnt.
process-para.
If ((field1 = ws-field1) and (field2=ws-field2))
add +1 to counter
else
move +1 to counter
move field1 to ws-field1
move field2 to ws-field2
end-if
perform write-para.
inital value of working storage fields is spaces. Here i am trying to do a match with the previous field values, if match is found , i increment the counter else i move 1 to the counter. But i m stuck at how to write the record with the count value of duplicates records whenever a unique record is encountered. I also need to write if the there''s is only one occurance of the record. in that case the counter will contain value as 1.
I don't have my actual code with me right now, but its similar to this approach. |
|
Back to top |
|
|
xknight
Active User
Joined: 22 Jan 2008 Posts: 117 Location: Liberty city
|
|
|
|
D you need to just calculate the count or sort the records and display in o/p files,
To do that,
1) Should sort the i/p file for the binary sort.
2) Determine the count and sort the records eventually.
PSEUDOCODE:
1) SORT THE FILE
READ I/P FILE
AT END SET 'Y' TO EOF
WS-ARRAY1 PIC 9(9) OCCURS 999999.
ADD +1 WS-STOCK
MOVE WS-FIELD1 TO WS-ARRAY1(WS-STOCK)
MOVE WS-FIELD2 TO WS-ARRAY2(WS-STOCK)
< --REST OF THE PROCESS -->
2) NOW WE HAVE THE TOTAL RECORDS COUNT, AND FROM FILE THE RECORDS HAS BEEN MOVED TO THE OCCURANCE.
3) SHALL SORT THE RECORDS
PERFORM UNTIL SW-SORTED = 'N'
MOVE 1 TO WS-I-CNTR
PERFORM UNTIL WS-I-CNTR >= WS-J-CNTR
IF (( WS-FIELD1(I) & WS-FIELD2(I) > WS-FIELD1(I+1) & WS- FIELD2(I+1) ))
THEN
MOVE WS-FIELD1(I) = WS-FIELD1-TEMP
MOVE WS-FIELD1-(I+1) TO WS-FIELD1(I)
MOVE WS-FIELD1-TEMP TO WS-FIELD1(I+1)
MOVE 'N' TO SW-SORTED
ELSE
MOVE 'Y' TO SW-SORTED
END-IF
COMPUTE WS-I-CNTR = WS-I-CNTR + 1
END-PERFORM
COMPUTE J-CNTR = J-CNTR - 1
END-PERFORM
END-PERFORM
Hope this helps |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
baljinders wrote: |
But i want to do it through a cobol program...Also field 1 and Field2 are not contiguos i.e there are some other fields in between them...i should have mentioned it earlier... |
I want to ...
Hello, but what am I missing here. Surely the most efficient and readily available method of getting what you want is far easier than wasting the time and money of your employer in reinventing the wheel.
If your personal wants are so overwhelming then call your SORT product from a COBOL program and waste the minimum amount of resource. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Most often there is more to a requirement than simply counting by some key. . . Most often these "other parts of the requirement" are not posted in these topics (which often causes them to grow as the additional details of the requirement become known).
Also, many organizations do no permit "sort-grams" - the standard for the organization is one or more coding languages - not utility statements for things like sort, file-aid, etc.
When someone asks for help with a cobol (or some other language) requirement, i believe we should support this (after all most posters here are programmers of one kind or another) and not send them to a sort, file-aid, selcopy, etc solution.
I believe in exploiting the "programming features" of the sort products as well as other utilities, but i also believe that actually using code ia quite important also. |
|
Back to top |
|
|
baljinders
New User
Joined: 21 Aug 2006 Posts: 72
|
|
|
|
Hi All,
I wanted to use cobol because that was the requirement given to me. That's the reason i posted it on cobol section instead of sort utility section. I know sort would give me the desired result with minimum of effort. But sometimes we got to do things the way the organisation wants it done.
In my case it turned out that i was given wrong information and we are indeed allowed to use sort utility . Its just that we are not allowed to use Icetool.
So got my desired result using the below sort card.
SORT FIELDS=(1,3,CH,A,5,3,CH,A)
SUM FIELDS=(8,5,ZD)
INREC BUILD=(1,7,C'00001')
OUTREC BUILD=(1,7,C' ',8,5)
Thanks everyone for your time and help. You guys are simply awesome. I will stilll go ahead and try to use the psuedo code given by xavierrajnaveen
Thanks xavierrajnaveen |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Good to hear you have a solution - thank you for posting what you used
Quote: |
I will stilll go ahead and try to use the psuedo code given by xavierrajnaveen |
If there are any questions/problems, reply back here and someone will be able to help.
d |
|
Back to top |
|
|
|