Reading a record in [Length][Data][Length][Data]... format



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

Reading a record in [Length][Data][Length][Data]... format

Postby anand2283 » Sun Nov 04, 2012 12:44 pm

Hi,

I need to process a variable length record which is in [Length][Data][Length][Data].... format. Can someone please suggest a way to do it in Cobol. Example of data is:
X'00166B4B6B6B4B6B6B4B6B6B4B6B6B4B6B6B4B6B6B4B6B4B00216B4B6B6B4B6B6B4B6B6B4B6B6B4B6B6B4B6B6B4B6B.....
So, the basic theme is read the length which is X'16' = 22 then read 22 bytes of data, then length X'21' = 34, then read 34 bytes of data and so on.
It can be done easily in BAL, but wan't to have any suggestions to do it in COBOL.

I know, the data can be read using reference mod if, I can get to process length correctly. I am basically facing problem reading and converting the length part to decimal.

Thanks in Advance,
Anand.
Anand
anand2283
 
Posts: 4
Joined: Sun Nov 04, 2012 12:16 pm
Has thanked: 4 times
Been thanked: 0 time

Re: Reading a record in [Length][Data][Length][Data]... form

Postby BillyBoyo » Sun Nov 04, 2012 1:49 pm

I think you are confusing terminology.

When we "read" a variable-length record we use READ and the record appears for us and we can use the/an 01 under the FD for the file to access the record.

X'21' is 33, by the way.

Reference-modification has no problem using values in a binary field.

Can you exactly describe what it is you are trying to do?
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Reading a record in [Length][Data][Length][Data]... form

Postby NicC » Sun Nov 04, 2012 4:54 pm

However, Billy, if I understand correctly each record consists of fields that each have their own length field preceding - quite common for transmitting data. So each record is: rdwlengthField1Field1LengthField2Field2 etc
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Reading a record in [Length][Data][Length][Data]... form

Postby Robert Sample » Sun Nov 04, 2012 5:12 pm

Your length field is a 2-byte binary value, which can be defined in COBOL as
77 FIELD-LENGTH PIC S9(04) COMP (or COMP-5)
. Hence you could do something like this (untested code):
MOVE 1 TO OFFSET-VAR.
MOVE INPUT-VAR (OFFSET : 2) TO FIELD-LENGTH.
ADD 2 TO OFFSET.
MOVE INPUT-VAR (OFFSET : FIELD-LENGTH) TO <other variable>.
ADD FIELD-LENGTH TO OFFSET.
and repeat for the length of INPUT-VAR.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Reading a record in [Length][Data][Length][Data]... form

Postby anand2283 » Sun Nov 04, 2012 7:21 pm

BillyBoyo wrote:I think you are confusing terminology.

When we "read" a variable-length record we use READ and the record appears for us and we can use the/an 01 under the FD for the file to access the record.

X'21' is 33, by the way.

Reference-modification has no problem using values in a binary field.

Can you exactly describe what it is you are trying to do?




Billy...My apologies. It's indeed 33. However, Nicc understood it correctly. I tried reference mod for this, but when I try to move the length part to a comp or binary usage field, it simply changes the value of like X'0016' to a value of 06. Not sure what it's actually doing.
Anand
anand2283
 
Posts: 4
Joined: Sun Nov 04, 2012 12:16 pm
Has thanked: 4 times
Been thanked: 0 time

Re: Reading a record in [Length][Data][Length][Data]... form

Postby anand2283 » Sun Nov 04, 2012 7:25 pm

Robert Sample wrote:Your length field is a 2-byte binary value, which can be defined in COBOL as
77 FIELD-LENGTH PIC S9(04) COMP (or COMP-5)
. Hence you could do something like this (untested code):
MOVE 1 TO OFFSET-VAR.
MOVE INPUT-VAR (OFFSET : 2) TO FIELD-LENGTH.
ADD 2 TO OFFSET.
MOVE INPUT-VAR (OFFSET : FIELD-LENGTH) TO <other variable>.
ADD FIELD-LENGTH TO OFFSET.
and repeat for the length of INPUT-VAR.



Bob, actually, this is what I tried before posting my concern, but when I try to move the length part to a comp or binary usage field, it simply changes the value of like X'0016' to a value of 06. Not sure what it's actually doing.
Anand
anand2283
 
Posts: 4
Joined: Sun Nov 04, 2012 12:16 pm
Has thanked: 4 times
Been thanked: 0 time

Re: Reading a record in [Length][Data][Length][Data]... form

Postby Robert Sample » Sun Nov 04, 2012 8:22 pm

I did some testing and found you need to move the length to a PIC X(02) that is redefined as PIC S9(04) COMP for this code to work correctly. I suspect there's some conversion going on in the pseduoassembler generated by COBOL but haven't checked it yet. However, once this change was made the posted code worked exactly as it should.

These users thanked the author Robert Sample for the post:
anand2283 (Tue Nov 06, 2012 6:01 am)
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Reading a record in [Length][Data][Length][Data]... form

Postby BillyBoyo » Mon Nov 05, 2012 12:39 am

:-)

How many pieces of variable data can you have - a fixed number (so, what is it) or a variable number (so what is the maximum?). Where does the data come from? Is it on a file, or from somewhere else?

What is the data?
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Reading a record in [Length][Data][Length][Data]... form

Postby BillyBoyo » Mon Nov 05, 2012 1:24 am

       01  W-VARIABLE-FROM-RECORD-01       COMP PIC S9(4).
       01  W-VARIABLE-FROM-RECORD-02       COMP PIC S9(4).
       01  W-VARIABLE-FROM-RECORD-03       COMP PIC S9(4).
       01  W-RECORD-WITH-VARIABLE-PARTS.
           05  W-RWVP-LENGTH-01            COMP PIC S9(4).
           05  W-RWVP-DATA-01.
               10  FILLER OCCURS 0 TO 256 TIMES
                   DEPENDING ON W-VARIABLE-FROM-RECORD-01.
                   15  FILLER                   PIC X.
           05  W-RWVP-LENGTH-02            COMP PIC S9(4).
           05  W-RWVP-DATA-02.
               10  FILLER OCCURS 0 TO 256 TIMES
                   DEPENDING ON W-VARIABLE-FROM-RECORD-02.
                   15  FILLER                   PIC X.
           05  W-RWVP-LENGTH-03            COMP PIC S9(4).
           05  W-RWVP-DATA-03.
               10  FILLER OCCURS 0 TO 256 TIMES
                   DEPENDING ON W-VARIABLE-FROM-RECORD-03.
                   15  FILLER                   PIC X.


     MOVE W-RWVP-LENGTH-01    TO W-VARIABLE-FROM-RECORD-01
     MOVE W-RWVP-LENGTH-02    TO W-VARIABLE-FROM-RECORD-02
     MOVE W-RWVP-LENGTH-03    TO W-VARIABLE-FROM-RECORD-03
     DISPLAY W-RWVP-DATA-01
     DISPLAY W-RWVP-DATA-02
     DISPLAY W-RWVP-DATA-03

These users thanked the author BillyBoyo for the post:
anand2283 (Tue Nov 06, 2012 6:02 am)
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Reading a record in [Length][Data][Length][Data]... form

Postby BillyBoyo » Mon Nov 05, 2012 1:41 am

For the reference-modification, it treats the input as PIC X(variable) so the MOVE to a numeric assumes unsigned, so the x'F' clobbers the x'1'.

These users thanked the author BillyBoyo for the post:
anand2283 (Tue Nov 06, 2012 6:02 am)
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post