Saturday, November 19, 2011

TABLES


An OCCURS clause is used to indicate the repeated occurrences of items of the same format in a structure. OCCURS clause is not valid for 01, 77, 88 levels.
It can be defined as elementary or group item. Initialization of large table occurrences with specific values are usually done using perform loops in procedure division. Simple tables can be initialized in the following way.
01 WEEK-ARRAY VALUE ‘MONTUEWEDTHUFRISATSUN’.
               05 WS-WEEK-DAYS OCCURS 7 TIMES PIC X(03).

Dynamic array is the array whose size is decided during runtime just before the access of first element of the array.

01 WS-MONTH-DAY-CAL.
                   05 WS-DAYS OCCURS 31 TIMES DEPENDING ON WS-OCCURENCE.       

IF MONTH = ‘FEB’ MOVE ‘28’ to WS-OCCURRENCE.

Array Items can be accessed using INDEX or subscript and the difference between them are listed in the table. Relative subscripts and relative indexes are supported only in COBOL85. Literals used in relative subscripting/indexing must be an unsigned integer.
ADD WS-SAL(SUB) WS-SAL(SUB + 1) TO WS-SAL(SUB + 2).

Sl #
Subscript
Index
1
Working Storage item
Internal Item – No need to declare it.
2
It means occurrence
It means displacement
3
Occurrence, in turn translated to displacement to access elements and so slower than INDEX access.
Faster and efficient.
4
It can be used in any arithmetic operations or for display.
It cannot be used for arithmetic operation or for display purpose.
5
Subscripts can be modified by any arithmetic statement.
INDEX can only be modified with SET, SEARCH and PERFORM statements.

          Sometimes, you may face a question like how to randomly access the information in the sequential file of 50 records that contains all the designation and the respective lower and higher salary information.
          Obviously, OS does not allow you to randomly access the sequence file. You have to do by yourself and the best way is, load the file into a working storage table in the first section of the program and then access as you wish.

          The table look-up can be done in two ways.
          -Sequential search.
          -Binary search.

No comments:

Post a Comment