COBOL - Array/Table ProcessingArrays are referred to as tables in COBOL. An array is a linear data structure, which is a collection of individual data items of the same data type. The data items of a table are internally sorted. Table DeclarationIn the Data Division, we can declare the table, and we have to use the 'Occurs' clause to define a table. The Occurs clause is used to indicate the repetition of the data-name definition. It can only be used with level numbers starting from 02 to 49. For the Redefines, we can't use the Occurs clause. Description for the one-dimensional and two-dimensional table is given below: One-Dimensional TableIn a one-dimensional table, the ' Occurs ' clause should be specified only once in the declaration. Syntax: Here, WS-TABLE is the group item that contains the table, and WS-A names the table elements that occur ten times. Example: Let's see an example for a single dimensional table: Output: Two-Dimensional TableA two-dimensional table is generated with the variable length of both data elements. Syntax: Here, the first WS-A array may occur 1 to 10 times, and the inner WS-C array may occur 1 to 5 times. Means, there will be 5 WS-C entries for each WS-A entry. Example: Let's see an example for a two-dimensional table: Output: SubscriptBy using the subscript, we can retrieve the table's individual elements. The values of subscript will vary from 1 to the number of times that the table/array occurs. Any +ve number can be a subscript value. In the data division, there is no need for any subscript declaration. It is formed automatically with the Occurs clause. Example:Let's see an example to understand the subscript: Output: IndexWe may also use the index to access the table elements. An index is an element that moves from the beginning of the table. We need to describe the INDEXED BY clause with the Occurs clause to declare the index. Use the SET statement and the PERFORM VARYING option to change the index value. Syntax:Example:Let's see an example to understand the index in the table: Output: Set StatementThe set statement changes the index value. It is used to initialize, increment, or decrement the index value. This statement can be used with search and search all to locate elements in the table. Syntax:Example:Let's see an example for a set statement: Output: SearchIt is a linear method of searching. This is used for locating table elements. We can perform the search on a sorted or unsorted table. Search is used only for tables declared by index phrase. This begins with the index's initial value. If the searched item is not available, the index will be automatically incremented by one and will continue until the end of the table. Example:Let's see an example for Search: Output: Search AllSearch All is a binary search method. This is used to find elements inside the table. The table must be in sorted order in Search All. The index does not need to be initialized. As we know, in the binary search method, the table is divided into two half sections, and it determines in which half the searched element is present. This process repeats until the element is found or the end is reached. Example:Let's see an example for Search All: Output: Next TopicCOBOL File Handling |