Page 1 of 1

read a VSAM file from bottom to top and length of a string using COBOL?

Posted: Thu Apr 18, 2024 1:51 pm
by Prakash Jha
Hi,

Can someone help me

1. How to read a VSAM file from bottom to top.
2. How to find the exact length of a string using COBOL?

Re: read a VSAM file from bottom to top and length of a string using COBOL?

Posted: Thu Apr 18, 2024 7:06 pm
by Robert Sample
Assuming you mean in reverse order when you say "from bottom to top" -- this cannot be done in Enterprise COBOL without adding a sequence number to the data set and sorting by the sequence number (descending).

Repeat after me:  "COBOL DOES NOT HAVE STRINGS!"  Look at the PICTURE for the variable.  The length of the variable in the PICTURE clause is the length of the variable -- COBOL will add blanks to the end to force the length to be the PICTURE length.  While you can emulate strings using OCCURS DEPENDING ON, such variables do NOT behave like strings in other languages.

Re: read a VSAM file from bottom to top and length of a string using COBOL?

Posted: Fri Apr 19, 2024 5:03 am
by zum13
Hello.

COBOL does have an intrinsic function called LENGTH, which can be used like this:

Code: Select all

MOVE FUNCTION LENGTH(WA-INPUT-FIELD) TO WA-LENGTH
This will give you the length of the alphanumeric field or group field defined in working storage. It won't do anything clever like working out the length of the field without trailing spaces, however, it will take into consideration multi-byte national character sets.

Fields in COBOL have a static length. The main reason for using the LENGTH function would be if you expect field lengths to change over time and don't want the bother of tracking hard-coded lengths.

The ALLOCATE statement does have the ability to create areas of storage of arbitrary length, but the description in the manuals is a little unspecific about what happens of you use LENGTH against such an area. Unfortunately, I've not had opportunity to use this feature, but the implication is that you'd get back the length of the area allocated.

Re: read a VSAM file from bottom to top and length of a string using COBOL?

Posted: Mon Apr 22, 2024 2:12 pm
by Prakash Jha
Robert Sample" wrote: Thu Apr 18, 2024 7:06 pm Assuming you mean in reverse order when you say "from bottom to top" -- this cannot be done in Enterprise COBOL without adding a sequence number to the data set and sorting by the sequence number (descending).

Repeat after me:  "COBOL DOES NOT HAVE STRINGS!"  Look at the PICTURE for the variable.  The length of the variable in the PICTURE clause is the length of the variable -- COBOL will add blanks to the end to force the length to be the PICTURE length.  While you can emulate strings using OCCURS DEPENDING ON, such variables do NOT behave like strings in other languages.
 
Repeating after you, "COBOL DOES NOT HAVE STRINGS!". Actually it was asked this way so I had written it this way. Sorry if that was wrong.

For VSAM, if use STRARTBR from last record, then if we do -1, can that create the effect of reverse reading?

Re: read a VSAM file from bottom to top and length of a string using COBOL?

Posted: Mon Apr 22, 2024 2:14 pm
by Prakash Jha
zum13Hello.

COBOL does have an intrinsic function called LENGTH, which can be used like this:

Code: Select all

MOVE FUNCTION LENGTH(WA-INPUT-FIELD) TO WA-LENGTH
This will give you the length of the alphanumeric field or group field defined in working storage. It won't do anything clever like working out the length of the field without trailing spaces, however, it will take into consideration multi-byte national character sets.

Fields in COBOL have a static length. The main reason for using the LENGTH function would be if you expect field lengths to change over time and don't want the bother of tracking hard-coded lengths.

The ALLOCATE statement does have the ability to create areas of storage of arbitrary length, but the description in the manuals is a little unspecific about what happens of you use LENGTH against such an area. Unfortunately, I've not had opportunity to use this feature, but the implication is that you'd get back the length of the area allocated.
So even if we use LENGTH, to find the length of actual characters, we have to use STRINGs and UNSTRINGs only?

Re: read a VSAM file from bottom to top and length of a string using COBOL?

Posted: Wed Apr 24, 2024 7:15 am
by zum13
Yes. COBOL does not handle character data in the same way as something like C where there is a string termination character (the null). If you do

Code: Select all

MOVE 'A' TO WA-FIELD-1
and WA-FIELD-1 is 40 characters, COBOL will move the 'A' followed by 39 spaces. You have to rely on other means to determine that there's only one non-space character in the field.

The statement you're probably better off using is INSPECT which has TALLYING options for calculating the lengths of things. There's also the "REVERSE" function which will reverse the characters in a field (don't overwrite the original!) which means that you can do something like the following to measure a field without the trailing spaces:

Code: Select all

       DATA DIVISION.
                                                                   
       WORKING-STORAGE SECTION.
                                                                   
       01  WA-FIELD-1                     PIC X(40) VALUE 'A'.
       01  WA-TEMP                        PIC X(40).
       01  WA-COUNTER                     PIC S9(4) COMP.
       01  WA-COUNTER-DISP                PIC 9(4).
                                                                   
       PROCEDURE DIVISION.

           MOVE FUNCTION REVERSE(WA-FIELD-1) TO WA-TEMP

           INSPECT WA-TEMP TALLYING WA-COUNTER FOR LEADING SPACE

           COMPUTE WA-COUNTER-DISP =
                   LENGTH OF WA-FIELD-1 - WA-COUNTER

           DISPLAY WA-COUNTER-DISP

           GOBACK
           .
It's convoluted, I know, but COBOL wasn't really designed to work this way.

(and I'd completely forgotten about "LENGTH OF"!)