Page 1 of 1
Include only part of input in output.
Posted: Sun Dec 15, 2024 8:49 pm
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?
Re: Include only part of input in output.
Posted: Mon Dec 16, 2024 7:07 am
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.
Re: Include only part of input in output.
Posted: Thu Dec 19, 2024 11:25 am
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?
Re: Include only part of input in output.
Posted: Thu Dec 19, 2024 8:19 pm
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!