Compare records in a file and move the least values to outpu



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

Compare records in a file and move the least values to outpu

Postby gyt3 » Thu Feb 14, 2013 11:47 pm

Hi ,
I need help in coding a cobol program .The requirement goes like this

Input file has three fields . i.e Value A, Value B and Value C. For every unique Value A and Value C combination , I need smallest Value B.

Sample Input file

abcd  10000 905
abcd  10      905
abcd  9000  906
pqrs   100    906
pqrs    100  906


For Value a = abcd and Value c = 905 combination , the smallest Value b is "10".So the output file should be like "abcd 10 905"
Similarly , For Value a = abcd and Value c = 906 combination , the smallest Value b is 9000, So the output file should be like " abcd 9000 906".


Expected Output file :
abcd 10  905
abcd 9000 906
pqrs 100 906


Am a beginner in Cobol and Can someone help me how to code for this requirement in COBOL.

Code'd
gyt3
 
Posts: 15
Joined: Sun Dec 16, 2012 9:58 am
Has thanked: 3 times
Been thanked: 0 time

Re: Compare records in a file and move the least values to outpu

 

Re: Compare records in a file and move the least values to o

Postby enrico-sorichetti » Fri Feb 15, 2013 12:09 am

where are You facing problems ...
the logic or the coding ???
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2643
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: Compare records in a file and move the least values to o

Postby gyt3 » Fri Feb 15, 2013 7:51 am

in d logic.
gyt3
 
Posts: 15
Joined: Sun Dec 16, 2012 9:58 am
Has thanked: 3 times
Been thanked: 0 time

Re: Compare records in a file and move the least values to o

Postby Akatsukami » Fri Feb 15, 2013 5:10 pm

You did not enclose your sample data in Code tags, so it is necessary to begin by asking: are the three fields of fixed size, or are they delimited by spaces?
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day
User avatar
Akatsukami
Global moderator
 
Posts: 1053
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Compare records in a file and move the least values to o

Postby c62ap90 » Fri Feb 15, 2013 10:30 pm

For your example second record (abcd 10 905)
will the "10" be '10 ' (3-spaces) or
will the "10" be '00010' (3-zeros)?
c62ap90
 
Posts: 125
Joined: Thu Oct 11, 2012 10:24 pm
Has thanked: 1 time
Been thanked: 6 times

Re: Compare records in a file and move the least values to o

Postby BillyBoyo » Fri Feb 15, 2013 10:52 pm

If you are able to sort your file on A, C, B, it will help a lot.

You then just need the first record which causes a "control break", with A and C being the control fields treated as one. You'd not need to even test the value in any way, just write the first record of the "break".

I always prefer and "external" sort, but if you are learning Cobol, try with a Cobol SORT. The only problem you may face is if the data is not in fixed locations on your input file, which is unclear from your sample.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Compare records in a file and move the least values to o

Postby Akatsukami » Fri Feb 15, 2013 10:54 pm

Akatsukami wrote:You did not enclose your sample data in Code tags, so it is necessary to begin by asking: are the three fields of fixed size, or are they delimited by spaces?

A moderator kindly coded the data. It's rather sloppily typed, but I shall assume that the fields are of fixed length. The question (implied by c62ap90's question) of whether the comparison is to be numeric or character is still open; again, I shall assume it is to be numeric.

  1. Sort the data set by fields A and C, and right-justify field B.
  2. Initialize a "least value" field to 99,999 . Initialize "last A" and "last C" fields to HIGH-VALUES.
  3. Read a record. On a break in A and/or C, report the working storage fields (if A and C are not HIGH-VALUES); move A and C to the "last" fields; set "least value" to B.
  4. If no break, then if "least value " > B, move B to "least value".
  5. At EOF, report the last set ofworking storage values.
There's your logic. Go for it.
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day

These users thanked the author Akatsukami for the post:
gyt3 (Fri Feb 15, 2013 11:22 pm)
User avatar
Akatsukami
Global moderator
 
Posts: 1053
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Compare records in a file and move the least values to o

Postby gyt3 » Fri Feb 15, 2013 11:24 pm

c62ap90 wrote:For your example second record (abcd 10 905)
will the "10" be '10 ' (3-spaces) or
will the "10" be '00010' (3-zeros)?

there wont be any 0's. the value wil be 10
gyt3
 
Posts: 15
Joined: Sun Dec 16, 2012 9:58 am
Has thanked: 3 times
Been thanked: 0 time

Re: Compare records in a file and move the least values to o

Postby gyt3 » Fri Feb 15, 2013 11:26 pm

BillyBoyo wrote:If you are able to sort your file on A, C, B, it will help a lot.

You then just need the first record which causes a "control break", with A and C being the control fields treated as one. You'd not need to even test the value in any way, just write the first record of the "break".

I always prefer and "external" sort, but if you are learning Cobol, try with a Cobol SORT. The only problem you may face is if the data is not in fixed locations on your input file, which is unclear from your sample.

hi, the input is a csv file of variable length
gyt3
 
Posts: 15
Joined: Sun Dec 16, 2012 9:58 am
Has thanked: 3 times
Been thanked: 0 time

Re: Compare records in a file and move the least values to o

Postby BillyBoyo » Sat Feb 16, 2013 1:21 pm

Is this a "learning" task? They've not made it simple for you.

Whichever way you go, you'll need the data in fixed locations for comparisons. The most simple way to get that in Cobol is with UNSTRING.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post