Home » COBOL Array/Table Processing

# COBOL – Array/Table Processing

Arrays 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 Declaration

In 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 Table

In 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 Table

A 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: ## Subscript

By 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: ## Index

We 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.

### Example:

Let’s see an example to understand the index in the table:

Output: ## Set Statement

The 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.

### Example:

Let’s see an example for a set statement:

Output: ## Search

It 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 All

Search 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:
{"ft_lang_label":"__label__en","ft_lang_prob":0.8008943,"math_prob":0.68498194,"size":3238,"snap":"2023-14-2023-23","text_gpt3_token_len":708,"char_repetition_ratio":0.1462585,"word_repetition_ratio":0.044802867,"special_character_ratio":0.21124151,"punctuation_ratio":0.120300755,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95139813,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-21T18:00:28Z\",\"WARC-Record-ID\":\"<urn:uuid:6f191cc9-86ed-466c-97d3-6e5690e58c3f>\",\"Content-Length\":\"409934\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:646e9354-cbb0-4e98-8555-8c1cc1e1d2f5>\",\"WARC-Concurrent-To\":\"<urn:uuid:d70884e2-a56d-45c2-bd8d-180a37e459b4>\",\"WARC-IP-Address\":\"104.21.62.147\",\"WARC-Target-URI\":\"https://www.tutoraspire.com/cobol-array-or-table-processing/\",\"WARC-Payload-Digest\":\"sha1:JMQLNJIENGBJQQZ2ILT2XYWH7JEEXVEZ\",\"WARC-Block-Digest\":\"sha1:LPBV7KHICCQ4KZPRHM25X2D3JMH75UYP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296943704.21_warc_CC-MAIN-20230321162614-20230321192614-00144.warc.gz\"}"}