IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Appending a variable with values separated by commas


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
rakesh17684

New User


Joined: 08 Oct 2006
Posts: 61
Location: San Diego

PostPosted: Wed Jan 14, 2009 11:12 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Wed Jan 14, 2009 11:38 pm
Reply with quote

How is variable K1 defined? Do you care about extra spaces around the comma?
Back to top
View user's profile Send private message
rakesh17684

New User


Joined: 08 Oct 2006
Posts: 61
Location: San Diego

PostPosted: Wed Jan 14, 2009 11:44 pm
Reply with quote

K1 is defined numerical. spaces around comma is not a problem. Just looking for a simple append mechanism in COBOL
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Thu Jan 15, 2009 12:23 am
Reply with quote

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
View user's profile Send private message
rakesh17684

New User


Joined: 08 Oct 2006
Posts: 61
Location: San Diego

PostPosted: Thu Jan 15, 2009 12:43 am
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Jan 15, 2009 12:54 am
Reply with quote

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
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Thu Jan 15, 2009 12:55 am
Reply with quote

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
View user's profile Send private message
rakesh17684

New User


Joined: 08 Oct 2006
Posts: 61
Location: San Diego

PostPosted: Thu Jan 15, 2009 6:26 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Jan 15, 2009 6:56 pm
Reply with quote

So use 3 instead of 6 as the increment to I.
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Thu Jan 15, 2009 8:25 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Jan 15, 2009 8:26 pm
Reply with quote

Good point, Terry. I should have thought of that when I was coding it up.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Null values are considered in Total c... DFSORT/ICETOOL 6
No new posts Variable Output file name DFSORT/ICETOOL 8
Search our Forums:

Back to Top