Include only part of input in output.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Seema Jain
New Member
Posts: 6
Joined: Sun Dec 20, 2015 11:58 pm

Include only part of input in output.

Post by Seema Jain »

If I have an input file containing data like 12345578, and I want the output to include only 5578, what would the logic for this be in COBOL?
User avatar
zum13
Active Member
Posts: 135
Joined: Thu May 04, 2023 12:58 am

Re: Include only part of input in output.

Post by zum13 »

Hello.

There are a couple of ways of doing it, assuming that everything is working on fixed positions.

The first is that you can split the input field into two parts, for example:

Code: Select all

       01  WA-INPUT-RECORD.
           03  WA-INPUT-FIELD.
               05  WA-INPUT-FIELD-START  PIC X(4).
               05  WA-INPUT-FIELD-END    PIC X(4).
           03  FILLER                    PIC X(72).
You then move WA-INPUT-FIELD-END to the output record.

The alternative is to substring the field in a MOVE statement:

Code: Select all

           MOVE WA-INPUT-FIELD (5:4) TO WA-OUTPUT-FIELD
which will copy four characters from WA-INPUT-FIELD starting at position five to WA-OUTPUT-FIELD.
Seema Jain
New Member
Posts: 6
Joined: Sun Dec 20, 2015 11:58 pm

Re: Include only part of input in output.

Post by Seema Jain »

zum13
The alternative is to substring the field in a MOVE statement:

Code: Select all

           MOVE WA-INPUT-FIELD (5:4) TO WA-OUTPUT-FIELD
which will copy four characters from WA-INPUT-FIELD starting at position five to WA-OUTPUT-FIELD.
Thank you zum13. I've used this, will this work for Variable records also?
User avatar
zum13
Active Member
Posts: 135
Joined: Thu May 04, 2023 12:58 am

Re: Include only part of input in output.

Post by zum13 »

Yes, it will work, although it is incumbent upon you to make sure that the thing you are substringing is within the length of the current record otherwise you may well get some odd results!
Post Reply Previous topicNext topic

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.”