×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

COBOL General discussion FAQ

Cobol Code Examples

How do I convert comma-delimited records? by k5tm
Posted: 30 Jul 04 (Edited 8 Feb 06)

Perhaps you have someone supplying data in a comma-separated (CSV) file.  This might help.

CODE

       identification division.
       program-id.  unstring-fields.
       data division.
       working-storage section.
       01  binary.
           02  I               PIC S9(4).
           02  J               PIC S9(4).
       01  the-delimiter       PIC X(3).             
       01  INPUT-FIELD  PIC X(123) VALUE
           'Bill Smith,"444-55-6666",999999999,1234567,"$1,060.46 "'.
       78  INPUT-FIELD-SIZE value LENGTH OF INPUT-FIELD.

       01  .
           02  OCCURS 40.
               03  FIELD-HOLDER PIC X(50).
               03  FIELD-LENGTH PIC 9(4).
       procedure division.
       a.
           MOVE 1 to I.
           MOVE 1 TO J.
           PERFORM UNTIL I > INPUT-FIELD-SIZE
               INSPECT INPUT-FIELD (I:) TALLYING I FOR LEADING SPACE
               IF I NOT > INPUT-FIELD-SIZE
                   EVALUATE INPUT-FIELD (I:1)
                   WHEN '"'
                       ADD 1 TO I
                       IF I NOT > INPUT-FIELD-SIZE
                           UNSTRING INPUT-FIELD
                               DELIMITED BY '",' OR '"'
                               INTO FIELD-HOLDER (J)
                               DELIMITER IN THE-DELIMITER
                               COUNT IN FIELD-LENGTH (J)
                               POINTER I
                           END-UNSTRING
                           ADD 1 to J
                       END-IF
                   WHEN OTHER
                       UNSTRING INPUT-FIELD DELIMITED BY ","
                           INTO FIELD-HOLDER (J)
                           DELIMITER IN THE-DELIMITER
                           COUNT IN FIELD-LENGTH (J)
                           POINTER I
                       END-UNSTRING
                       ADD 1 to J
                   END-EVALUATE
               END-IF
           END-PERFORM.
           SUBTRACT 1 FROM J.
           stop run.
Note that this code provides the skeleton of scanning comma-delimited records, including detecting fields that are enclosed in quotation marks (").  Subsequent conversion of numeric values, embedded quotation marks, and other anomolies you might find in 'real life' are not addressed.

Back to COBOL General discussion FAQ Index
Back to COBOL General discussion Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close