Page 1 of 1

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

PostPosted: Thu Feb 14, 2013 11:47 pm
by gyt3
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

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

PostPosted: Fri Feb 15, 2013 12:09 am
by enrico-sorichetti
where are You facing problems ...
the logic or the coding ???

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

PostPosted: Fri Feb 15, 2013 7:51 am
by gyt3
in d logic.

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

PostPosted: Fri Feb 15, 2013 5:10 pm
by Akatsukami
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?

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

PostPosted: Fri Feb 15, 2013 10:30 pm
by c62ap90
For your example second record (abcd 10 905)
will the "10" be '10 ' (3-spaces) or
will the "10" be '00010' (3-zeros)?

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

PostPosted: Fri Feb 15, 2013 10:52 pm
by BillyBoyo
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.

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

PostPosted: Fri Feb 15, 2013 10:54 pm
by Akatsukami
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.

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

PostPosted: Fri Feb 15, 2013 11:24 pm
by gyt3
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

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

PostPosted: Fri Feb 15, 2013 11:26 pm
by gyt3
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

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

PostPosted: Sat Feb 16, 2013 1:21 pm
by BillyBoyo
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.