You are on page 1of 7

Summary

Single-level arrays can be considered as lists of identically formatted (elementary or


group) data items. These data items are referenced by specifying their position in the
list through the use of a single subscript (or index). Double-level arrays, also
called two-dimensional arrays, are more like a spreadsheet, where two subscripts (row
and column) are necessary to reference a single data item. Higher level arrays can
also be used in COBOL, with three subscripts required for a three-dimensional array,
and so on.
Declaring a Two-level Array
Multiple-level arrays are declared in the DATA DIVISION (in either the FILE or
WORKING-STORAGE SECTIONs) through use of multiple OCCURS clauses. Multiple-level
arrays may consist of elementary or group items.
Arrays of Similar Elementary Items
Assume the sequential file TEMP-FILE contains one week (seven days) of daily
temperature readings in this format:
TEMP-REC
+------+------+-+------+
| 1am | 2am | ... | 12am |
+------+------+-+------+
1
4 5
8
93 96
Each of the fields represents a temperature reading taken at hourly intervals during a
single day (at 1:00am, 2:00am, and so on). Each field is in a four-character format,
leading sign format: +056, -003, etc. These temperatures are to be read from a file
and moved to a two-level array that conceptually looks like a table with 7 rows and 24
columns:
1am
2am
...
12am
+------+------+-+------+
day 1 |
|
| ... |
|
+------+------+-+------+
day 2 |
|
| ... |
|
+------+------+-+------+
...
+------+------+-+------+
day 7 |
|
| ... |
|
+------+------+-+------+
Each elementary data item has the same format, and can be referenced by specifying its
row and column position. The array can be declared as:
01 ARRAY-AREA.
05 WEEK-DAY OCCURS 7 TIMES.
10 TEMP OCCURS 24 TIMES
PIC S9(03)
SIGN IS LEADING SEPARATE.
The group item WEEK-DAY is an array (it OCCURS 7 TIMES). Each occurence of WEEK-DAY is
an array TEMP, consisting of 24 four-character elementary data items. Since the OCCURS
defining the day appears first, it is called the "major-level" occurs; the "minorlevel" OCCURS describes the hour, since it appears second in the array definition. The
array could have also been defined to contain 24 rows and 7 columns:
01 ALT-ARRAY-AREA.
05 HOUR
OCCURS 24 TIMES.
10 ALT-TEMP
OCCURS 7 TIMES PIC S9(03)
SIGN IS LEADING SEPARATE.
Note: The PIC S9(03) designates a signed, three digit integer. The SIGN IS ...
SEPARATE clause causes COBOL to treat the sign of the number as a separate (in this
case LEADING) character, rather than be stored as part of the number itself, so each
data item occupies four characters of storage. A separate sign can also be specified
as TRAILING.
Either or both of the OCCURS clauses in a two-level array may be assigned an index:
01 ARRAY-AREA.
05 WEEK-DAY OCCURS 7 TIMES
INDEXED BY XD.
10 TEMP
OCCURS 24 TIMES

INDEXED BY XT
PIC S9(03)
SIGN IS LEADING SEPARATE.
Arrays of Mixed Elementary Items
In the examples above, the record data consisted only of identical temperature
readings. If each record contained additional fields, such as the date of the reading,
the array would need to be declared differently. Assume the record format is:
TEMP-REC
+----------+------+------+-+-------+
| YY/MM/DD | 1am | 2am | ... | 12am |
+----------+------+------+-+-------+
1
8 9
12 13 16
101 104
If each record is to be loaded into an array that includes the date, this declaration
would be appropriate:
01 ARRAY-AREA.
05 DAY-DATA OCCURS 500 TIMES
INDEXED BY J.
10 TEMP-DATE
PIC X(08).
10 HOUR-DATA OCCURS 24 TIMES
INDEXED BY K
PIC S9(03)
SIGN IS LEADING SEPARATE.
In this declaration, DAY-DATA is a an array of 500 group data items. Each group item
say DAY-DATA (2)contains one scalar alphanumeric variable TEMP-DATE, and an array
HOUR-DATA of 24 elementary numeric data items. Both DAY-DATA and HOUR-DATA are
assigned indexes.
Accessing Elements of Two-Level Arrays
An elementary data item of a two-level array is referenced by use of two subscripts in
parentheses following the data item name. The subscripts must be in the same order as
their related OCCURS clauses: major first, then minor. To initialize the 6:00am
temperature on Tuesday:
MOVE 0 TO TEMP (2, 6).
(Using the ARRAY-AREA definition)
MOVE 0 TO TEMP (6, 2).
(Using ALT-ARRAY-AREA)
Group items are referenced using the appropriate number of subscripts (or indexes) for
their dimensions:
MOVE ZEROS TO ARRAY-AREA.
WRITE OUT-REC FROM WEEK-DAY (J).
Assigning Values to Elements of Two-level Arrays
Values may be assigned to elements of a multiple-level array by reading them from a
file, MOVEing them into a group or elementary data element of the array, or as the
target of an arithmetic operation. Assume TEMP-FILE (with record format as above) has
this FD:
FD TEMP-FILE.
01 TEMP-REC
PIC X(96).
Records may be read directly into the array defined as ARRAY-AREA:
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 7
READ TEMP-FILE
AT END
DISPLAY 'BAD INPUT FILE'
STOP RUN
NOT AT END
MOVE TEMP-REC TO WEEK-DAY (J)
END-READ
END-PERFORM.
Processing Two-level Arrays
Each elementary data item of a two-level must be referenced by two subscripts (or
indexes), reflecting its "row" and "column" position in the array. If all array
elements are to be processed, this can be done with nested PERFORM VARYING loops. The
outer loop controls the major subscript, and the inner loop the minor. For example, to
compute the average temperature for a single day J:
MOVE 0 TO AVG-TEMP

PERFORM VARYING K FROM 1 BY 1 UNTIL K > 24


ADD TEMP (J, K) TO AVG-TEMP
END-PERFORM.
COMPUTE AVG-TEMP ROUNDED = AVG-TEMP / 24.
DISPLAY 'AVG TEMP FOR DAY ', J, ' IS ', AVG-TEMP.
In this example, the J subscript (determining the day) is constant, while the K
subscript (the hour) is varied from 1 to 24. To compute the average temperature for
all seven days, put the above code inside an outer loop that will vary the J
subscript:
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 7
MOVE 0 TO AVG-TEMP
PERFORM VARYING K FROM 1 BY 1 UNTIL K > 24
ADD TEMP (J, K) TO AVG-TEMP
END-PERFORM
COMPUTE AVG-TEMP = AVG-TEMP / 24
DISPLAY 'AVG TEMP FOR DAY ', J, ' IS ', AVG-TEMP
END-PERFORM.
Processing of two-level arrays containing mixed data types is similar. This code
computes and displays an average daily temperature and the date (using the DAY-DATA
mixed array defined above, and assuming that NBR-OF-DAYS has been set to the number of
records read into the array):
PERFORM VARYING J FROM 1 BY 1 UNTIL J > NBR-OF-DAYS
MOVE 0 TO AVG-TEMP
PERFORM VARYING K FROM 1 BY 1 UNTIL K > 24
ADD HOUR-DATA (J, K) TO AVG-TEMP
END-PERFORM
COMPUTE AVG-TEMP = AVG-TEMP / 24
DISPLAY 'AVG TEMP FOR ', TEMP-DATE (J), ' IS ', AVG-TEMP
END-PERFORM.

Sample Programs
This program shows how to declare, read values into and process a two-level array of
elementary data items using subscripts. You will need the test data file TEMPS95 to
run this program.
IDENTIFICATION DIVISION.
PROGRAM-ID.
MARRAY1.
AUTHOR.
LOGICAL-EXTENSIONS.
DATE-WRITTEN.
960111 orig sjrcc RM-COBOL.
010123 upd fccj MF-COBOL.
****
*
*
*

This program shows how to use a two-dimensional array


to hold data. The program reads a file of hourly
temperatures for seven days, and computes and
displays the average temperature for each day.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT TEMP-FILE
ASSIGN TO DISK 'C:\COBOL\TESTDATA\TEMPS95.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
**** Each record holds 24 signed, three-digit hourly temperatures;

*
*

the temperatures are treated as four-character alpha-numerics


in the file definition below.
FD
01

TEMP-FILE.
TEMP-REC

PIC X(96).

WORKING-STORAGE SECTION.
01

ARRAY-AREA.
05 WEEK-DAY
10 TEMP
05

01

OCCURS 7 TIMES.
OCCURS 24 TIMES
PIC S9(03)
SIGN IS LEADING SEPARATE.
TABLE-SUBSCRIPTS.
10 J
PIC 9.
10 K
PIC 99.

AVG-TEMP

PIC S9(4)
SIGN IS LEADING SEPARATE.

/
PROCEDURE DIVISION.
000-MAIN-LINE.
PERFORM 100-READ-ARRAY
PERFORM 200-CALC-TEMP
STOP RUN.
100-READ-ARRAY.
**** Input temperature array data
OPEN INPUT TEMP-FILE.
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 7
READ TEMP-FILE
AT END
DISPLAY "BAD INPUT FILE"
STOP RUN
NOT AT END
MOVE TEMP-REC TO WEEK-DAY (J)
END-READ
END-PERFORM
CLOSE TEMP-FILE.

200-CALC-TEMP.
**** Since array is doubly-subscripted, will use a nested
*
PERFORM to compute average for each day
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 7
MOVE 0 TO AVG-TEMP
PERFORM VARYING K FROM 1 BY 1 UNTIL K > 24
ADD TEMP (J, K) TO AVG-TEMP
END-PERFORM
COMPUTE AVG-TEMP = AVG-TEMP / 24
DISPLAY "AVG TEMP FOR DAY ", J, " IS ", AVG-TEMP
END-PERFORM.

Depicts the processing of a two-level array of mixed data element types with indexes.
You will need the test data file TEMPS96 to run this program.
IDENTIFICATION DIVISION.
PROGRAM-ID.
MARRAY2.
AUTHOR.
LOGICAL-EXTENSIONS.
DATE-WRITTEN.
960128 orig sjrcc RM-COBOL.
010123 upd fccj MF-COBOL.
****
*
*
*

This program shows how to use a two-level array to hold


data. The program reads a file of hourly temperatures for
a series of dates, and computes and displays the date and
average temperature for each date.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT TEMP-FILE
ASSIGN TO DISK 'C:\COBOL\TESTDATA\TEMPS96.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
**** Each record holds a 8-character date (YY/MM/DD) followed by
*
24 signed, three-digit hourly temperatures; the temperatures
*
are treated as four-character alpha-numerics in the FD below
FD
01

TEMP-FILE.
TEMP-REC.
05 DATE-IN
05 TEMPS-IN

PIC X(08).
PIC X(96).

WORKING-STORAGE SECTION.
01

ARRAY-AREA.
05 DAY-DATA
10 TEMP-DATE
10 HOUR-DATA

01

NBR-OF-DAYS

01

AVG-TEMP

01

EOF-INDICATOR
88 EOF

OCCURS 500 TIMES


INDEXED BY J.
PIC X(08).
OCCURS 24 TIMES
INDEXED BY K
PIC S999
SIGN IS LEADING SEPARATE.
PIC 999

VALUE 0.

PIC S9(04)
SIGN IS LEADING SEPARATE.
PIC X

VALUE 'N'.
VALUE 'Y'.

/
PROCEDURE DIVISION.
000-MAIN-LINE.
PERFORM 100-READ-ARRAY.
PERFORM 200-CALC-TEMP.
STOP RUN.

100-READ-ARRAY.
**** Input temperature array data
OPEN INPUT TEMP-FILE
PERFORM VARYING J FROM 1 BY 1
UNTIL J > 500 OR EOF
READ TEMP-FILE
AT END
SET EOF TO TRUE
NOT AT END
MOVE TEMP-REC TO DAY-DATA (J)
ADD 1 TO NBR-OF-DAYS
END-READ
END-PERFORM
CLOSE TEMP-FILE.
200-CALC-TEMP.
**** Since array is doubly-subscripted, will use a nested
*
PERFORM to compute average for each day
PERFORM VARYING J FROM 1 BY 1 UNTIL J > NBR-OF-DAYS
MOVE 0 TO AVG-TEMP
PERFORM VARYING K FROM 1 BY 1 UNTIL K > 24
ADD HOUR-DATA (J, K) TO AVG-TEMP
END-PERFORM
COMPUTE AVG-TEMP = AVG-TEMP / 24
DISPLAY 'AVG TEMP FOR ', TEMP-DATE (J), ' IS ', AVG-TEMP
END-PERFORM.