COBOL Programming in 1-hour

This morning I read about 60 year old COBOL being still alive and there was a need for programmers. I couldn’t believe it so I checked the job boards and I found 2 jobs in my immediate area.

I thought that I’d spend an hour or so looking into COBOL. Ok I’m definitely no way near an expert, but I was surprised how much I was able to pick-up.

Getting Started

Traditionally Cobol (Common Business Oriented Language) only ran on mainframe computers. Now luckily you can install a full working environment on Windows and Linux.

If you are working in Windows there are few options: NetCobol, GnuCobol, and the Hercules Emulator

I work primarily in Linux and found that OpenCobol was very easy to use. If you’re working in Ubuntu, Debian or on a Raspberry Pi enter:

sudo apt-get install open-cobol

An Input/Output Program

A Cobol program starts with 2 lines, and an IDENTIFICATION , and a PROGRAM-ID. Comments lines start with a *. All Cobol programming lines end with a period (.).

For this example 4 variables were defined in the DATA layout section. The PIC (picture) argument is used to define the type of variables X is alpha, and 9 is numeric.

*>  test2.cbl - get user input, and do output
*>	  
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-ID PIC 9(3) VALUE 101.
   01 WS-STUDENT-NAME PIC X(25).
   01 WS-DEPT PIC X(25) VALUE 'Engineering'.
   01 WS-DATE PIC X(10).

PROCEDURE DIVISION.
   DISPLAY "Enter Your Name:".
   ACCEPT WS-STUDENT-NAME.
   ACCEPT WS-DATE FROM DATE.
   DISPLAY " ".
   DISPLAY "Name :  " WS-STUDENT-NAME " ID: " WS-ID.
   DISPLAY "Dept :  " WS-DEPT.
   DISPLAY "Date :  " WS-DATE.

STOP RUN.

For this example the ID and Dept are predefined. The WS-DATE is set by today’s DATE.

The WS-STUDENT-NAME is entered by the user.

To compile and run the program (ctest2.cbl) :

$ cobc -free -x -o ctest2 ctest2.cbl
pete@lubuntu:~/Writing/Blog/cobol$ ./ctest2
Enter Your Name:
Pete
 
Name :  Pete                      ID: 101
Dept :  Engineering              
Date :  210701    

Math

Cobol uses VERBs for math and conditional statements. Below is an example doing some basic math with an IF condition:

*> cmath.cbl - do some math with an IF condition
*>
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 A PIC 9(5) VALUE 10.
   01 B PIC 9(5) VALUE 20.
   01 C PIC 9(5) VALUE 0.
   01 D PIC 9(5) VALUE 0.
   01 E PIC 9(5) VALUE 0.
	  
PROCEDURE DIVISION.
   ADD A B TO C.
   MULTIPLY A BY B GIVING D.
   IF D > 100 THEN
      MOVE D TO E
   ELSE
	  MOVE 99 TO E
   END-IF.
   
   DISPLAY "C = " C.
   DISPLAY "D = " D.
   DISPLAY "E = " E.
STOP RUN.

Note for the IF statement there is no end of statement (.) until END-IF.

To compile and run this program:

$ cobc -free -x -o cmath cmath.cbl
$ ./cmath

C = 00030
D = 00200
E = 00200

File I/O – Read

Cobol has a number of read/write formats. The easiest is a fixed field sequential read/write.

For an example with input.txt , the STUDENT-ID is 5 characters and the name is 25.

20003 Albert Smith            
20004 Malika Jones            
20005 Bob Smith 

The Cobol code has a FILE section that defines the input fields. A WORKING-STORAGE section mimic the input file structure, with the exception of a end of File variable.

The STUDENT file object is written into the WS-STUDENT object until an END of file is set.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
      FILE-CONTROL.
      SELECT STUDENT ASSIGN TO 'input.txt'
      ORGANIZATION IS LINE SEQUENTIAL.            

DATA DIVISION.
   FILE SECTION.
   FD STUDENT.
   01 STUDENT-FILE.
      05 STUDENT-ID PIC 9(5).
      05 NAME PIC A(25).

   WORKING-STORAGE SECTION.
   01 WS-STUDENT.
      05 WS-STUDENT-ID PIC 9(5).
      05 WS-NAME PIC A(25).
   01 WS-EOF PIC A(1). 

PROCEDURE DIVISION.
   OPEN INPUT STUDENT.
      PERFORM UNTIL WS-EOF='Y'
         READ STUDENT INTO WS-STUDENT
            AT END MOVE 'Y' TO WS-EOF
            NOT AT END DISPLAY WS-STUDENT
         END-READ
      END-PERFORM.
   CLOSE STUDENT.
STOP RUN.

File I/O – Append (Write)

Using the input.txt file, the code below (cwrite.cbl) will add 2 more records:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
      FILE-CONTROL.
      SELECT STUDENT ASSIGN TO 'input.txt'
      ORGANIZATION IS LINE SEQUENTIAL. 
  
DATA DIVISION.  
    FILE SECTION.   
    FD STUDENT.
    01 STUDENT-FILE.
      05 STUDENT-ID PIC 9(5).
      05 NAME PIC A(25). 
 
  
PROCEDURE DIVISION.  
    DISPLAY 'WRITING TO A SEQUENTIAL FILE..'  
    OPEN EXTEND STUDENT.  
    MOVE '20006' TO STUDENT-ID.  
    MOVE ' Santa Claus' TO NAME.   
    WRITE STUDENT-FILE  
    END-WRITE. 
      
    MOVE '20007' TO STUDENT-ID.  
    MOVE ' Mary Christmas' TO NAME.  
    WRITE STUDENT-FILE  
    END-WRITE. 
      
    CLOSE STUDENT.  
STOP RUN.

To compile, run and check the output:

$ cobc -free -x -o main cwrite.cbl
$ ./main
WRITING TO A SEQUENTIAL FILE..
$ cat input.txt
20003 Albert Walker 
20004 Malika Jones
20005 Bob Cat
20006 Santa Claus
20007 Mary Christmas

Final Comments

After coding in other languages it becomes immediately obvious that Cobol would require many more lines of code than it would in Python.

Learning Cobol does not appear to be overly difficult, however there are a ton of little nuances that will need to be flushed out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s