![]() |
VOOZH | about |
Arrays comes under the processing of "Linear data structure". It is a collection of single data items that can be referred by a single data name. Data items contained in an array are called as its elements. Elements of an array are internally stored in contiguous memory locations To access elements of the array, will use INDEX or SUBSCRIPT
The declaration is similar to any other data item an array is defined in the DATA DIVISION. To specify the repeated occurrence of the data item with the same format, the OCCURS clause can be used only with levels number 02 to 49. Using OCCURS clause we can specify the maximum number of elements that can be stored in the array
In the array/table we can access the element of an array by using SUBSCRIPT. The subscript can be a positive integer. The subscript must be enclosed within a pair of parenthesis. The highest value that the subscript can take is the integer value specified in the OCCURS clause. The lowest value is implicitly assumed to be 1.
We use the INDEX BY clause to initialize the value. We won't define the index by clause in the working-storage section. To modify the value of an index the SET verb is used. It will represent a displacement from the address of the first element
Example 1:
Output:
Example 2:
Output:
Syntax:
SET index-1 , . . . . To { index-2, identifier-2, integer-1 }.
Or
SET index-1, . . . . { UP BY, DOWN BY } { integer, identifier }.
Example 3:
Output:
The SEARCH keyword is used to check for the presence or absence of any particular elements in any tables or arrays. We can find an element by using the SEARCH keyword. It performs the Linear search using table names as indexes.
Syntax:
SEARCH table-name [ VARYING index ]
[AT END imperative statement-1 ]
{ WHEN condition-1 {statement-2, NEXT SENTENCE } }
[ END- SEARCH ].
Same to the search in the COBOL, SEARCH ALL is used to find the presence of any particular element in the table. Unlike the search, it won't perform a Linear search. Search All will work like "Binary Search" using table names as indexes.
Syntax:
SEARCH ALL table-name [ VARYING index ]
[AT END imperative statement-1 ]
{ WHEN condition-1 {statement-2, NEXT SENTENCE } }
[ END- SEARCH ].
Example 4:
Output: