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

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Prakash Jha
Registered Member
Posts: 62
Joined: Sat Jun 29, 2013 1:45 pm
India

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

Post 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?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

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

Post 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.
User avatar
zum13
Registered Member
Posts: 84
Joined: Thu May 04, 2023 12:58 am

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

Post 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.
Prakash Jha
Registered Member
Posts: 62
Joined: Sat Jun 29, 2013 1:45 pm
India

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

Post 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?
Prakash Jha
Registered Member
Posts: 62
Joined: Sat Jun 29, 2013 1:45 pm
India

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

Post 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?
User avatar
zum13
Registered Member
Posts: 84
Joined: Thu May 04, 2023 12:58 am

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

Post 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"!)
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”