The COBOL-85 Tutorial, Chapter 5

Data definitions in COBOL, part 2.

Copyright © 1998-2017

Kim Kjærsulf.


This chapter (TOC)

Records.
Qualifying a name.
Edited data fields.
Print lines.
MOVE ... CORR ...

Records.

[This chapter] [TOC] [Tutorial]


COBOL has good facilities for defining of structured variables - the equivalent of a Pascal record or a struc in C.
The structure is achieved via level numbering, by using higher and sub-levels.

The principles involved can best be illustrated through an example:
(a)	01  A-RECORD.
(b)	     03  FIELD-1	            PIC XXX.
(c)	     03  SUB-RECORD.
(d)	           05  FIELD-2       PIC 999.
(e)	           05  FIELD-3       PIC X(20).
(f)	     03  FIELD-4	            PIC 9(4).

A line-by-line explanation:
(a)
Level number 01, signals the start of a field and a structure. As this is not a PICTURE description it can only be a structure which is then described in detail in the subsequent lines. A MOVE instruction to and from such an 01 level is considered by the compiler as being an alphanumeric move with length equal to the sum of the length of the subjacent fields. If you use this kind of group level moving, you must be sure that the sending and the receiving structure is subdivided exactly the same way. There is done no conversion between data formats, when moving on the group level.
(b)
FIELD-1 is an alphanumeric field containing three characters, and is physically placed at the start of the record.
(c)
As this is not a PICTURE description it can only be a structure which is then described in detail in the subsequent lines. Same ruling applies to rule as for the 01 level above.
(d)
FIELD-2 is a numeric field containing three characters, without a sign. The field is placed immediately after FIELD-1.
(e)
FIELD-3 is an alphanumeric field containing 20 characters. The field is placed immediately after FIELD-2.
Length of SUB-RECORD is 3 + 20 characters.
(f)
FIELD-4 is placed outside SUB-RECORD, but inside A-RECORD. A-RECORD is 31 characters long. If one of the numeric fields has had a further type definition (e.g. COMP), this will make calculating the length considerably more difficult as the size of a COMP field is machine-dependent.


Qualifying a name.

[This chapter] [TOC] [Tutorial]


It is not usually necessary to take any special measures when referring to a field in a record, but if several records contain fields with the same name it is necessary to qualify the name with the name of the record:

		MOVE ZERO TO FIELD-2 IN A-RECORD.
This corresponds to the period notation in Pascal. See also MOVE ... CORR ... below.


Edited data fields.

[This chapter] [TOC] [Tutorial]


Field editing (=data presentation) is definitely one of COBOL's strong points.
There are many details involved here, and you are referred to the manual for these. However, here is a small sample of the possibilities available:

Se also : Print lines.
	77  num-ed	pic ---.-9.99.
	...
	move zero to num-ed.
	display num-ed.                 result :       0.00
	move 125 to num-ed.             result :     125.00
	move 1.25 to num-ed.            result :       1.25
	move 1250 to num-ed.            result :   1,250.00
	move -1250 to num-ed.           result :  -1,250.00

	77  num-ed	pic ---.--9Vbb99 blank when zero.
	...
	move zero to num-ed.
	display num-ed.                 result :
	move 125 to num-ed.             result :     125   00


	77  num-ed	pic ***.**9Vbb99 blank when zero.
	...
	move zero to num-ed.
	display num-ed.                 result :
	move 125 to num-ed.             result : ****125   00


Print lines.

[This chapter] [TOC] [Tutorial]


The combination of RECORD structures and edited files is used when constructing print lines which are to be printed out one line at a time. The entire line is formed in the memory before it is "sent" to the printer in the same way as a record is written to a file (more on this in the chapter called "Sequential files in COBOL").

Here is an example of print line construction:
	01  DETAIL-LINE.
	     03  FILLER	            PIC XXX             VALUE SPACE.
	     03  ACCOUNT            PIC 99B/B9999       BLANK WHEN ZERO.
	     03  FILLER             PIC XXX             VALUE " | ".
	     03  ACCT-NAME          PIC X(20).
	     03  FILLER             PIC X(5)            VALUE SPACE.
	     03  AMOUNT             PIC --,---,-9.99.
As can be seen from this example the name FILLER appears three times within the same record structure. This is possible because the name FILLER is not "referenceable" in COBOL. In this example the three FILLER fields are used to create space between the printed information.

The addition "BLANK WHEN ZERO" after the ACCOUNT field ensures that only blanks are written if the account number is zero.

The behaviour of the commas and the period in the mask for the AMOUNT field is controlled by the optional parameter DECIMAL-POINT IS COMMA in the configuration section.



MOVE ... CORR ...

[This chapter] [TOC] [Tutorial]


Assume that the starting point for the above print line is a record with the following construction:

	01  DETAIL-LINE.
	     03  FILLER	            PIC XXX             VALUE SPACE.
	     03  ACCOUNT            PIC 99B/B9999       BLANK WHEN ZERO.
	     03  FILLER             PIC XXX             VALUE " | ".
	     03  ACCT-NAME          PIC X(20).
	     03  FILLER             PIC X(5)            VALUE SPACE.
	     03  AMOUNT             PIC --,---,-9.99.
	01  DATA-RECORD.
	     03  ACCOUNT            PIC 9(6) COMP.
	     03  ACCT-NAME          PIC X(20).
	     03  AMOUNT             PIC 9(8)V99.
Then it is possible in COBOL to write:
		MOVE CORRESPONDING DATA-RECORD TO DETAIL-LINE.
This creates a MOVE during the compilation process for all fields with the same name and level number in DATA-RECORD and DETAIL-LINE. The normal rules for automatic data conversion is applied to each field.


Exercise 1.

[This chapter] [TOC] [Tutorial]


Enter, compile and run the examples in this chapter.


Copyright © 1998-2017

Kim Kjærsulf


Last Updated october 3, 2003
For more information contact: Kim Kjærsulf