Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

Module 9: Array processing and Table handling


OCCURS clause

  • In COBOL, Array/Table can be defined using OCCURS clause
  • General syntax:-

    OCCURS integer TIMES

  • Integer must be any positive numeric value
  • Some important consideration while using OCCURS clause are:-
    • OCCURS can be used at elementary or group level
    • OCCURS cannot be specified at 01, 66, 77 OR 88 LEVEL
    • Elements of array cannot be referred without subscript
    • While accessing array element, subscript value should range from 1 to integer value defined during declaration. If found outside of this range, program terminates
  • Example:- Like we saw in previous section ‘Single Dimension Array’, the best alternative for handling the storage of sales value for each quarter is:-

    01 SALES-LAPTOP. 05 SALES PIC 9(4) OCCURS 4 TIMES.

  • This method is more efficient than defining separate data items for each quarter (please refer example given in section ‘Single Dimension Table’)
  • Just imagine, If we had to store sales for each day of year! In such case without arrays, you may need 365 data items to be declared.

How array elements can be accessed?

  • Elements stored in array can be accessed by technique known as subscripting
  • First element can be accessed as ‘SALES(1)’, second element can be accessed as ‘SALES(2)’ and so on.
  • Value (1,2 etc.) is referred to as subscript
  • Some valid way of accessing array elements:-

    MOVE TEMP-SALES TO SALES(1) ADD SALES(1) TO TOTAL-SALES.

Using OCCURS at group level

  • Arrays cab be defined at group level using OCCURS clause
  • Example, suppose we have to store sales value and profit-value of laptops for each quarter of year. This requirement can be addressed using below data declaration

    01 SALES-LAPTOP OCCURS 4 TIMES. 05 SALES PIC 9(4). 05 PROFIT PIC 9(4).

  • Above declaration will create 4 occurrences of (SALES, PROFIT) elements
  • Conceptual view of above declaration

Value assignment to array elements

  • We can assign values to array elements using two methods:-
    • By storing value while declaring it
    • Using any data movement, arithmetic statements
  • First methods is little tricky to use as VALUE & OCCURS clause cannot appear together. Thus following example expresses the way we can use VALUE clause to assign values to array :-
    • Example 1:-

      01 COUNTRY-ARRAY ‘IndiaJapanItalySpain’. 05 COUNTRY-NAME PIC X(5) OCCURS 4 TIMES.

      It will initialize array elements as COUNTRY-NAME(1) = ’India’, COUNTRY-NAME(2) = ’Japan’, COUNTRY-NAME(3) = ’Italy’ and COUNTRY-NAME(4) = ’Spain’
    • Example 2:-

      01 COUNTRY-VALUE. 05 FILLER PIC X(5) VALUE ‘India’. 05 FILLER PIC X(5) VALUE ‘Japan’. 05 FILLER PIC X(5) VALUE ‘Italy’. 05 FILLER PIC X(5) VALUE ‘Spain’. 01 COUNTRY-NAME REDEFINES COUNTRY-VALUE. 05 COUNTRY PIC X(5) OCCURS 4 TIMES.

      REDEFINES clause cause array COUNTRY-NAME and COUNTRY-VALUE share same memory area internally. Thus, causing COUNTRY(1) initialized with value ‘India’, COUNTRY(2) =’Japan’ and so on.
  • Second method to assign value is by using any data movement or arithmetic statement
    • Example:-

      ADD SALES-QTY(2) TO TOTAL-SALES MOVE TEMP-SALES TO SALES-QTY(3).

Using OCCURS clause with DEPENDING ON

  • Sometimes you may encounter situation where number of occurrences are not fixed and is dependent on some other parameters. Using OCCURS clause along with DEPENDING ON phrase this kind of situation can be tackled
  • Basic syntax:-

    OCCURS integer-1 TO integer-2 TIMES DEPENDING ON data-item-1

  • Where,
    • integer-1, integer-2 must be positive integers. It is used to define minimum and maximum expected value of occurrences.
    • data-item-1 must be numeric data item
  • Example:-

    01 WS-MONTH-DAYS PIC 9(2). 01 WS-DAYS PIC 9(2) OCCURS 28 TO 31 TIMES DEPENDING ON WS-MONTH-DAYS.

  • In above example, WS-DAYS is variable length array. WS-DAYS can have minimum 28 occurrences and maximum 31 occurrences. Here, the final value of occurrences is decided at execution time and depends on WS-MONTH-DAYS. For example, if month is February then WS-MONTH-DAYS will have 28 days, thus total 28 occurrences of WS-DAYS will be created. Similarly, in case of APRIL, total 30 occurrences of WS-DAYS will be created and in case of December, 31 occurrences of WS-DAYS will be created.

Note: - OCCURS clause can also be used with ASCENDING/DESCENDING and INDEXED BY clause but that will be explained in upcoming section





© copyright mainframebug.com
Privacy Policy