You are on page 1of 6

Table Handling in COBOL In COBOL , a table is a group of data items which are having same data type.

The data items in table are logically related. Declaration Table can be declared by using OCCURS clause.

Eg: 01 TABLE-SKILLS. 02 TBL-SKILLS OCCURS 20 TIMES. 03 SKILL-NAME PIC X(10). Or 01 TABLE-SKILLS. 02 TBL-SKILLS Rules 1. Table or OCCURS can not be defined at 01 level. It can be defined from level number 02 to 49. 2. OCCURS can not coded at level numbers 01, 66, 77 and 88. 3. The subscripted data item (n) must be appositive integer. Subscripted Table The Subscript always refers to the occurrence of the table. The lowest value of the subscript is 1. The literal or data-item can be used as a data-item. If you are using data-item as a subscript, then the data-item must be elementary positive integer and it must be defined in WORKING-STORAGE SECTION. The most efficient format for subscript is COMP. 77 I PIC S 9(4) COMP. The subscript can be incremented or decremented. Reference modification can also be sued to move the data to elements of table. 1 MYTABLE. 05 TABLE-ELEMENT PIC X(5) OCCURS 5 TIMES. MOVE 00 TO TABLE-ELEMENT(1) (3:2). The above statement will move 00 to first element of table TABLEELEMENT and place the character at position 3 and length is 2. Indexed Table

PIC X(10) OCCURS 20 TIMES.

An index can be created by using INDEXED BY phrase of OCCURS clause. 05 TABLE-ITEM PIC X(8) OCCURS 10 INDEXED BY INX-A. An index is register. Indexed data-item need not be defined in WORKING-STORAGE SECTION. I t will be store as a register. The compiler calculates the value contained in the index as the occurrence number (subscript) minus 1, multiplied by length of table element. The fifth occurrence of TABLE-ITEM is the binary value contained in index INXA is (5-1) * 8 =32. An Indexed variable can be incremented or decremented or initialized by using SET verb. SET INX-A TO 1 OR SET INX-A UP 1 BY 1.

Variable - Length Tables. These type of tables will know the length of the tables( or no.of elements in the table) at run time. The OCCURS DEPENDING ON (ODO) clause can be used to define variablelength tables. Eg. 1 MAIN-AREA. 03 REC-1. 05 FILED-1 PIC 9. 05 FIELD-2 OCCURS 1 TO 10 TIMES DEPENDING ON FIELD-1. 1 REC-2. 5 REC DATA.. Search Table COBOL table can be searched in two ways. Linear search: This can be done by using SEARCH statement. Binary search: This can be done by using SEARCH ALL statement. Linear search: The SEARCH statement can be used to perform linear search at the beginning of the index. This is called sequential search. Syntax SEARCH table-name [VARYING identifier] AT END statements WHEN conditions Statements END-SEARCH. To use SEARCH statement the table must be defined with INDEXED BY phrase along with OCCURS clause. The SEARCH statement can be used for single dimension tables only.

Multiple WHEN conditions can be coded. The conditions in the WHEN phrase are evaluated in the order which they appear. If none of the condition is satisfied, then index is increased to next element of the table, and WHEN conditions are evaluated again. If any one of the WHEN conditions satisfied, then the SEARCH ends and index remains pointed to element of the table, which satisfied the condition. If entire table has been searched, none of WHEN condition is satisfied, then AT END statement will be executed. If AT END is not coded, then the control paused to next line in the program.

Binary search The SEARCH ALL statement can be used to perform binary search.. Binary Search is most efficient when you are dealing with large tables. Algorithm: Binary search will look at first data-item in the table. If match is found Then search is done. If match is not found, then it checks to see the data-item you are trying to find is smaller or larger than middle element. If it is larger, then we know that we only have to look in the second half of the table. If it is smaller, then we know that we only have to look in first half of the table. Based on this decision, we have eliminated half of the table from search. We will next look at the middle element of the remaining half and compare it to the element. This will either find a match or eliminate another quarter of the table. Note that in establishing what the middle is if the number of elements is even, the programmer of a binary search can choose to round or truncate. This process continues until either a match is found or it is clear that the element is not in the table.

The Table must be in sorted on some key for the binary search. The key field may be element itself or element of the group. The index need not be necessary to set to 1 prior to starting search. The index is always associated with the first index-name in the OCCURS clause.

For Binary search the OCCURS clause of the table must have key clause in addition to the INDEXED BY clause. The KEY IS phrase identifies the data-item on which the table is sorted.

Possible Interview Questions on Tables 1. How do define Dynamic array in COBOL how do u define single dimensional array and multidimensional array in our COBOL? Ans: In COBOL variable length tables can be created. No dynamic arrays. 01 SINGLE-DIM-ARRAY 05 SDA-ITEM PIC X OCCURS 30. A single-dimension array of thirty one-byte characters. 01 MULTI-DIM-ARRAY. 05 MDA-FIRST-DIM OCCURS 20. 07 MDA-SECOND-DIM OCCURS 30 PIC X. A multi-dimension array of 20 times 30 one-byte characters. 2. 01 WS-TABLE. 03 WS-TABLE-EL OCCURS 5 TIMES. 04 FILLER-X PIC X(1) VALUE 'A'. 04 WS-EX REDEFINES FILLER-X PIC X(1). What would be the output of DISPLAY WS-TABLE? Ans: 'AAAAA'. The code will compile and execute as Redefinition of an item subordinate to OCCURS clause. . 3. Which SEARCH verb is equivalent to PERFORM...VARYING? Ans: The serial SEARCH verb (SEARCH without ALL) 4. Can a SEARCH be applied to a table which does not have an INDEX defined? Ans: No, the table must be indexed. 5. What are the different rules applicable to perform a serial SEARCH? Ans: The SEARCH can be applied to only a table which has the OCCURS clause and INDEXED BY phrase, Before the use of the SEARCH the index must have some initial value. To search from beginning, set the index value to 1. Use the SEARCH verb without ALL phrase 6. What are the different rules applicable to perform a binary SEARCH? Ans: The table must be sorted in ascending or descending order before the beginning of the SEARCH. Use OCCURS clause with ASC/DESC KEY IS dataname1 option The table must be indexed. There is no need to set the index value. Use SEARCH ALL verb.

7. Can I display the index ?. Ans: Yes. SET DIS-VARIABLE RO INDEX-VARIBLE. Then DISPLAY DIS-VARIABLE. 8.

You might also like