View previous topic :: View next topic
|
Author |
Message |
bijal.awhad
New User
Joined: 19 Mar 2008 Posts: 51 Location: Pune
|
|
|
|
Hi All,
I was asked below question.
There is a PS file which contains 10 records. Data is as below
Code: |
1
2
3
4
5
6
7
8
9
10 |
Now Output file is needed which should look like below
Code: |
1 6
2 7
3 8
4 9
5 10 |
How it can be achieved using cobol?
I answered as below:
I will read first five records in array say ARRAY-1 then last five records in ARRAY-2. so ARRAY-1 & ARRAY-2 will look like below
ARRAY-1
ARRAY-2
STEP1: I will first move first record of ARRAY-1 into working storage variable like
Code: |
MOVE ARRAY-1(IDX1) TO WS-OP(1:1) |
STEP2: Then Move first record of ARRAY-2 into working storage variable like
Code: |
MOVE ARRAY-2(IDX2) TO WS-OP(2:1) |
STEP 3: Write in output file from WS-OP
STEP 4: Increment IDX1 & IDX2. Repeat step 1 , step 2 & step 3 until end of both the array i.e. IDX1 & IDX2 are greater than 5.
Please suggest me a better or alternate ways to achieve this? |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
well you are shooting yourself in the foot using reference modification,
since the 10th record consists of 2 char and not 1.
also, cobol has COBOL Internal Tables not arrays.
an array is a host variable structure for db2.
use proper structure/element definitions.
Code: |
WORKING-STORAGE SECTION.
01 output-rec.
05 record-part-1 pic x(zz).
05 record-part-2 pic x(zz).
01 cobol-internal-table.
05 half-of-table pic s9(4) comp.
05 cobol-internal-table-counter pic s9(4) comp value zero.
05 cobol-internal-table-item occurs 20 times
indexed by idx-1
idx-2
pic x(zz).
|
Code: |
open input input
open output output
Perform read-input
until eof
divide cobol-internal-table-counter by 2 giving half-of-table
perform write-output
varying idx-1
from 1
by 1
until idx-1 > half-of-table
close input, output
GOBACK.
READ-INPUT SECTION.
add 1 to cobol-internal-table-counter
set idx-1 to cobol-internal-table-counter
read input-file into cobol-internal-table-item(idx-1)
continue.
999-exit.
exit.
WRITE-OUTPUT SECTION.
SET IDX-2 to idx-1
set idx-2 up by half-of-table
move cobol-internal-table-item(idx-1) to record-part-1
move cobol-internal-table-item(idx-2) to record-part-2
write output from output-rec
continue.
999-exit.
exit.
|
and of course the PIC X(zz) should be the same |
|
Back to top |
|
|
bijal.awhad
New User
Joined: 19 Mar 2008 Posts: 51 Location: Pune
|
|
|
|
Dick,
Thanks for the detail solution. Very well thought
.
-------
Bijal |
|
Back to top |
|
|
Bang_1
New User
Joined: 08 May 2009 Posts: 39 Location: Bangalore
|
|
|
|
Dick,
Is that idx-2 in the below perform??
Quote: |
perform write-output
varying idx-1
from 1
by 1
until idx-1 > half-of-table |
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
What happens when you run it on your system? |
|
Back to top |
|
|
bijal.awhad
New User
Joined: 19 Mar 2008 Posts: 51 Location: Pune
|
|
|
|
Bang_1 wrote: |
Dick,
Is that idx-2 in the below perform??
Quote: |
perform write-output
varying idx-1
from 1
by 1
until idx-1 > half-of-table |
|
It's idx-1 only. Check it again. |
|
Back to top |
|
|
Bang_1
New User
Joined: 08 May 2009 Posts: 39 Location: Bangalore
|
|
|
|
Yeah, that should be IDX-1, I just finished validating with IDX-1 and then with IDX-2 in perform. I got confused with the SET statements in write-output. Thanks for correcting me. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
amend:
Code: |
divide cobol-internal-table-counter by 2 giving half-of-table |
to:
Code: |
divide cobol-internal-table-counter by 2 giving half-of-table rounded |
that will enable an number of input from 1 to 50 to be output.
thx to Bill for bringing this to my attention.
and there is nothing picky about it. the old divide would not account properly for an odd number of input records. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
The requirement was for 10 records, for which it works perfectly. Also for even number of records up to 20 (size of table).
One more thing for the odd number, set the table to blanks up front, or some method in the code to modify the right-column processing the final time through. When index 1 is 5 (for 9 items) index 2 (5 + 5) is pointing at whatever state the table was in after the linkedit.
Bang_1, make sure you fully understand it now, if it confused you. If you need two (or more) entries from a table at the same time, you need two (or more) indexes/subscripts. Enusure you fully grasp the differences between indexes and subscripts. |
|
Back to top |
|
|
|