Page 1 of 2

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

PostPosted: Sun Nov 04, 2012 12:44 pm
by anand2283
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.

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

PostPosted: Sun Nov 04, 2012 1:49 pm
by BillyBoyo
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?

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

PostPosted: Sun Nov 04, 2012 4:54 pm
by NicC
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

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

PostPosted: Sun Nov 04, 2012 5:12 pm
by Robert Sample
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.

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

PostPosted: Sun Nov 04, 2012 7:21 pm
by anand2283
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.

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

PostPosted: Sun Nov 04, 2012 7:25 pm
by anand2283
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.

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

PostPosted: Sun Nov 04, 2012 8:22 pm
by Robert Sample
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.

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

PostPosted: Mon Nov 05, 2012 12:39 am
by BillyBoyo
:-)

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?

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

PostPosted: Mon Nov 05, 2012 1:24 am
by BillyBoyo
       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

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

PostPosted: Mon Nov 05, 2012 1:41 am
by BillyBoyo
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'.