'FD' stands for File Descriptor, and names the file, INPUT-FILE (assigned in the environment
division), and describes the exact structure of the data in each record. All records in this file MUST be
of exactly the same structure.
'01 CUSTOMER-DATA' is the group name and refers to all of the single record that is read into
the computer memory from the file. The higher numbers (levels), 03.. and 05.. will contain the indivual fields
of the record.
Both FD and 01 are written in area A while higher levels are in area B.
Level 01 is sub-grouped into level 03 fields. Notice that one of the level 03 sub-groups is itself
sub-grouped into level 05. The sub-grouping could continue upwards as required to 07, 09 etc.. These numbers
(except level 01) could as easily be 02, 03, 04 ...or any increasing number scale.
The PIC (short for PICTURE) clause indicates the size and type of data that that field contains. For example,
in line 000450, the data name (identifier) NAME has been defined as holding 12 characters of alphnumeric data. It could have been
written as PIC XXXXXXXXXXXX be that's a pain. 'X' means alphnumeric and can contain any ASCII character. However, even if it contained
'2' you could not do any calculations on this as the information is stored as the ASCII code for the character '2', rather than the actual
number 2. Line 000470 defines HOUSE-NUMBER as PIC 9(2), which can hold a 2-digit number.
You can do calculations with this since '9' is used to denote a numeric field.
Notice how the group names (CUSTOMER-DATA and ADDRESS) do not have PIC descriptions. This is because the higher
level field descriptions when added together will be the size of the group name, i.e. CUSTOMER-NUMBER will hold 46 characters
which turns out to be the size of each record (spaces are included). You can refer to these group names but when doing so
all data will be treated as alphanumeric and cannot be used for calculations, even if all of the higher group items are numeric.
The WORKING-STORAGE SECTION of the data division is for defining data that is to be stored in temporary memory, i.e. during
program run-time. Effectively, this is where, for example, an identifier is defined that will hold the result of a calculation.
Also see the string to be displayed on the screen is actually
defined in working-storage using the VALUE clause:
000800 01 TOTALS-IN.
000810 03 1ST-NO PIC 99 VALUE ZERO.
000820 03 2ND-NO PIC 999 VALUE 100.
The equivalent to filling an item such as 1ST-NO (above) with zeroes, is filling an alphanumeric (PIC X) item with
spaces e.g. 01 MESSAGE PIC X(12) VALUE SPACES.
A further section of the data division is the LINKAGE SECTION, which is used to facilitate the sharing of data between
programs (or sub-programs). See below.
A program may also refer to a different program, called a sub-program. A sub-program is an entirely different program
from the calling program, with its own divisions etc... with the exception that it does not end with STOP RUN (which would
return you to the operating system), but with EXIT PROGRAM. The sub-program is a module, rather than a subroutine which is
what a paragraph could be described as. The verb CALL is used to activate the sub-program:
In the above code, a sub-program is called, named VALIDATE-DATE
In order to use data from the calling program in the sub-program
the calling program uses a section in the data division called the LINKAGE SECTION. The item W-DATE-IN
in the calling program occupies the same memory address as the sub-program's item L-DATE-IN, so the number placed in W-DATE-IN
item using the VALUE clause is also in L-DATE-IN. Note: you cannot use VALUE in the linkage section.
The procedure division of the sub-program requiring the use of linkage section defined data must say so by:
PROCEDURE DIVISION USING ...[linkage section items to be used] also refered to by the CALL ... USING. See lines 000930 and 3500 above.
In the above example, what is being called ("VALIDATE-DATE") is a literal. This means that you could use an identifier
instead, allowing you a choice between sub-programs depending on what the literal had been previously defined as.
For example, if a record was of type "A" then you may want to process that record using sub-program PROCESS-A-REC,
but if a type "B" record the use PROCESS-B-REC.
The logic might be as follows:
:
0003000 IF RECORD-TYPE = "A" THEN
0003010 MOVE "PROCESS-A-REC" TO SUB-PROG
0003020 ELSE MOVE "PROCESS-B-REC" TO SUB-PROG
0003030 CALL SUB-PROG USING L-REC-DATA
:
Although I haven't described the various commands of the procedure division, the above code is fairly clear...if a marker called RECORD-TYPE has been set as "A" then place (i.e. MOVE) the string "PROCESS-A-REC"
into the area of memory labelled as SUB-PROG (so now SUB-PROG contains this string). Otherwise (i.e. ELSE)
it is assumed that the only other type there is can be "B" type and so "PROCESS-B-REC" is MOVEd into SUB-PROG. Depending on
what the item SUB-PROG contains the desired sub-program will be called.
The sample code here was written quickly, so they aren't particularly well
structured. Also, they are not the usual type of COBOL program that you would normally come
across. COBOL is more likely written for business applications such as payroll programs
or stock control etc... Hopefully they might give an indication of how COBOL works.
This program is designed to add line numbers to COBOL code that has been
typed into a text editor (e.g. Notepad) in the following format:
:
PROCEDURE DIVISION.
MAIN-PARAGRAPH.
MOVE X TO Y
*the comment asterisk will be placed in position 7
/as will the page break solidus
IF Y > Z THEN
ADD Z TO X
MOVE X TO Z
ELSE DISPLAY 'The hypen for continuing a string
- 'onto the next line also goes into position 7'
END-IF
*all other text is placed from position 8
*so you still need to indent where required
STOP RUN.
*lastly, there is a limit of about
*70 characters per line (from position 8)
The text file containing COBOL code as above should be call named input.txt. Following execution,
the program will produce a new file called output.cob although it will still be a simply text file, but
can be compiled. The output.cob file for the above code would be:
:
000010 PROCEDURE DIVISION.
000020
000030 MAIN-PARAGRAPH.
000040 MOVE X TO Y
000050*the comment asterisk will be placed in position 7
000060/as will the page break solidus
000070 IF Y > Z THEN
000080 ADD Z TO X
000090 MOVE X TO Z
000100 ELSE DISPLAY 'The hypen for continuing a string
000110- 'onto the next line also goes into position 7'
000120 END-IF
000130*all other text is placed from position 8
000140*so you still need to indent where required
000150 STOP RUN.
000160
000170*lastly, there is a limit of about
000180*70 characters per line (from position 8)
This program is designed to refresh COBOL code line numbers following editing that would
result in uneven line number increases (or even no line number at all) where lines have been
inserted or deleted.
This is a little program that calulates prime numbers. You are prompted to enter
a number (up to 1999) and the program will produce a file, 'PRIME-NO.TXT,
which contains a table of all prime numbers up to the value entered.
This program takes a line sequential record file and converts it to an indexed file.
The records must contain a unique key field that is in strict ascending order. The input
file (from a text editor) should be called 'LINESEQFILE.TXT'. The program output will be 'INDEXEDFILE.DAT'.
You can change these in the ENVIRONMENT DIVISION if you want.
This program allows you to view the contents of an indexed file by generating a
line sequential file of the original indexed file. If you tried to open an indexed file
with a text editor you would just see gibberish. The input file for this program is
'INDEXEDFILE.DAT' giving an output text file called READFILE.TXT. Again, you can change these
in the ENVIRONMENT DIVISION if you wish.