filedatasetcobolmainframeflat

How to remove leading zeroes & trailing spaces in COBOL


Hi I have requirement to read a input file, each record contains certain record

01 FILE-DATA.
   05 FILE-FIELD-A     PIC 9(18).
   05 FILLER           PIC X(01) VALUE '*'.
   05 FILE-FILED-B     PIC X(30).
   05 FILLER           PIC X(01) VALUE '*'.

input data: 12345*ACBDE12345

when I unstring the file record, each file is getting 000000012345 (leading zeroes) & 'ABCDE12345 ' (trailing spaces).

Strangely the file data is send with such variable length into to fixed data structure, to tackle this how can I go about removing the leading zeroes and Trailing spaces.


Solution

  • For leading zeroes: use COBOL editing

     77 FILE-FIELD-EDITED  PIC Z(17)9.
    
    MOVE FILE-FIELD-A TO FILE-FIELD-EDITED
    

    To remove trailing spaces (or possibly leading ones on the FILE-FIELD-EDITED var): use the instrinsic TRIM function:

    STRING FUNCTION TRIM (FILE-FIELD-EDITED LEADING)
           '*'
           FUNCTION TRIM (FILE-FIELD-B TRAILING)
           DELIMITED BY SIZE
           INTO SOME-OUTPUT-FIELD
    END-STRING
    

    which would then give the output you've shown above.

    To learn more about editing and intrinsic functions see your COBOL implementation's language reference (that implementation may not have the LEADING and TRAILING option, for example) or for general COBOL the current COBOL draft standard.