View previous topic :: View next topic
|
Author |
Message |
rakesh17684
New User
Joined: 08 Oct 2006 Posts: 61 Location: San Diego
|
|
|
|
I want to find out a method how i can code this one in COBOL:
i fetch records one at a time from a DB2 table and for each fetch i append one of the column value to variable and then suffix with a comma excepting the last record fetched.
example:
open cursor1
fetch into cursor1 :K1
1st fetch,
var1 = K1
2nd fetch
var1= K1,K1
3rd fetch
var1 = K1,K1,K1
...etc.,
last fetch
var1 = K1,K1,K1,...K1
any tips for this will be greatly appreciable.
[/code] |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
How is variable K1 defined? Do you care about extra spaces around the comma? |
|
Back to top |
|
|
rakesh17684
New User
Joined: 08 Oct 2006 Posts: 61 Location: San Diego
|
|
|
|
K1 is defined numerical. spaces around comma is not a problem. Just looking for a simple append mechanism in COBOL |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
Will these resultant concatenated values of K1 separated by commas end up to be a variable length record to be written out? If so, I'd populate the record using reference modification. |
|
Back to top |
|
|
rakesh17684
New User
Joined: 08 Oct 2006 Posts: 61 Location: San Diego
|
|
|
|
nope iam not using this to create variable length records, but to compare fields in 2 files in JCL , where some times the number of fields to be compared will vary based on the fetch from table. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Reference modification will work fine. This code
Code: |
05 I PIC 9(04) VALUE 1.
05 WS-VAR1 PIC 999.99.
05 WS-VAR2 PIC 999.99.
05 WS-VAR-OUT PIC X(2048) VALUE SPACE.
LINKAGE SECTION.
/
PROCEDURE DIVISION.
S1000-MAIN SECTION.
MOVE 123.45 TO WS-VAR1.
MOVE 98.76 TO WS-VAR2.
MOVE WS-VAR1 TO WS-VAR-OUT (I : 6).
ADD 6 TO I.
MOVE ',' TO WS-VAR-OUT (I : 1).
ADD 1 TO I.
MOVE WS-VAR2 TO WS-VAR-OUT (I : 6).
ADD 6 TO I.
MOVE ',' TO WS-VAR-OUT (I : 1).
ADD 1 TO I.
MOVE WS-VAR1 TO WS-VAR-OUT (I : 6).
ADD 6 TO I.
MOVE ',' TO WS-VAR-OUT (I : 1).
ADD 1 TO I.
MOVE WS-VAR2 TO WS-VAR-OUT (I : 6).
ADD 6 TO I.
DISPLAY WS-VAR-OUT (1 : I) . |
produces this output:
Code: |
123.45,098.76,123.45,098.76 |
|
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
I guess my question concerning the record was unnecessary. I'd still use reference modification to populate VAR1, which needs to be large enough to receive the results of all the fetches. Instead of using K1,K1,K1,...K1 as your example output, please show a more realistic output example. |
|
Back to top |
|
|
rakesh17684
New User
Joined: 08 Oct 2006 Posts: 61 Location: San Diego
|
|
|
|
The output is same as robert as put above nonly that it is not in decimal and as the declaration of the 2 variable are:
WS-VAR1 PIC 999
WS-VAR2 PIC 999 |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
So use 3 instead of 6 as the increment to I. |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
Or better yet, use the LENGTH OF special register instead of a hard-coded number in case the picture sizes changes in the future.
Code: |
MOVE WS-VAR1 TO WS-VAR-OUT (I:LENGTH OF WS-VAR1)
ADD LENGTH OF WS-VAR1 TO I |
|
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Good point, Terry. I should have thought of that when I was coding it up. |
|
Back to top |
|
|
|