Microsoft KB Archive/57379

From BetaArchive Wiki

Example of Loading a COBOL Table (Array) from a Disk File

PSS ID Number: Q57379 Article last modified on 01-24-1990

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The information below shows the steps necessary to properly load a COBOL table from a file. An example is provided at the end to help illustrate the procedure. Note that this is not the only method to load a COBOL table. A COBOL table is a group of values that are referenced in the program by the table’s variable name. The table is a data structure equivalent to an array in BASIC or Pascal. The following information applies to Microsoft COBOL Versions 3.00 and 3.00a.

More Information: The use of tables provides a method to collect a group of similar elements together in an easy-to-manipulate data structure. Each element in a table can be referenced by a table variable that is a subscripted integer or an integer expression. To reference a table element, the table variable must be enclosed within parentheses. Additionally, the table’s variable name requires as many subscripts as there are dimensions in the table. For example, tab1 (10) references the tenth value in a one-dimensional table named tab1, while tab2 (5, 10) references a value in the fifth row, tenth column in a table named tab2. The maximum number of multiple dimensions a table can have is three in ANS74 and up to sixteen dimensions in later versions (unless the ANS85 directive is used, then the dimension limit is seven). The maximum number of tables is limited only by the computer’s memory. Furthermore, there are table-handling facilities provided to specify ascending or descending keys as well as permits to search a table’s dimension for an item satisfying a specified condition. The following are rules for specifying and using tables: 1. The OCCURS clause is used to define the amount of table dimensions and items. 2. The value of the dimension size must be 0 (zero) or greater. 3. The OCCURS clause cannot be specified in a data description entry that has a 66 or 88 level-number. 4. The OCCURS clause should not be specified in a data description entry at the 01 or 77 level-number (this restriction can be ignored through some directives). 5. The variable names must be unique words in the program. For a more detailed explanation, please refer to Chapter 4 in the “Microsoft COBOL Compiler 3.0: Language Reference Manual” for Versions 3.00 and 3.00a. The sample program below demonstrates this explanation by creating a file, loading that file into a table, and then printing the table values.

Code Example

  $SET ANS85
   IDENTIFICATION DIVISION.
   PROGRAM-ID.  LOAD-COBOL-TABLE.
  *
   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
        SELECT IN-FILE
          ASSIGN TO DISK
          ORGANIZATION IS SEQUENTIAL.
  *
        SELECT OUT-FILE
          ASSIGN TO DISK
          ORGANIZATION IS SEQUENTIAL.
  *
   DATA DIVISION.
   FILE SECTION.
  *
   FD  IN-FILE
        LABEL RECORDS ARE STANDARD
        VALUE OF FILE-ID IS "COBDATA.DAT"
        DATA RECORD IS INPUT-RECORD.
   01  INPUT-RECORD     PIC X(20).
  *
   FD  OUT-FILE
         LABEL RECORDS ARE STANDARD
         VALUE OF FILE-ID IS "COBDATA.DAT"
         DATA RECORD IS OUT-RECORD.
   01  OUT-RECORD         PIC X(20).
  *
   WORKING-STORAGE SECTION.
  *  INDIVIDUAL VARIABLES
   01 EOF          PIC XXX VALUE "NO".
   01 MYSTRING     PIC X(50) VALUE
           "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
   01 STALL        PIC X.
   01 CNT          PIC 99.
  *
   01 MYTABLE             OCCURS   10 TIMES.
      05 TAB-STRING     PIC X(10).
      05 TAB-NUMBER     PIC 9(10).
  *
  *  WORKING RECORDS
  *
   01 WORK-REC.
     05 A-STRING           PIC X(10).
     05 A-NUMBER           PIC 9(10).
  *
  *
   PROCEDURE DIVISION.
   000-MAIN.
       OPEN OUTPUT OUT-FILE.
       PERFORM 100-MAKE-FILE VARYING CNT FROM 1 BY 1
         UNTIL CNT > 10.
       COMPUTE CNT = 1.
       CLOSE OUT-FILE.
       OPEN INPUT IN-FILE.
       READ IN-FILE INTO WORK-REC
         AT END MOVE 'YES' TO EOF.
       PERFORM 200-LOAD-AND-SHOW-TABLE
         UNTIL EOF = 'YES'.
       CLOSE IN-FILE.
       STOP RUN.
  *
   100-MAKE-FILE.
       MOVE MYSTRING (CNT:CNT) TO A-STRING.
       MOVE CNT TO A-NUMBER.
       MOVE WORK-REC TO OUT-RECORD.
       WRITE OUT-RECORD.
  *
   200-LOAD-AND-SHOW-TABLE.
       MOVE A-STRING TO TAB-STRING (CNT).
       MOVE A-NUMBER TO TAB-NUMBER (CNT).
       DISPLAY TAB-NUMBER (CNT) "   " TAB-STRING (CNT).
       COMPUTE CNT = CNT + 1.
       READ IN-FILE INTO WORK-REC
         AT END MOVE 'YES' TO EOF.

Output

0000000001 A 0000000002 BC 0000000003 CDE 0000000004 DEFG 0000000005 EFGHI 0000000006 FGHIJK 0000000007 GHIJKLM 0000000008 HIJKLMNO 0000000009 IJKLMNOPQ 0000000010 JKLMNOPQRS Copyright Microsoft Corporation 1990.