Table Handling in COBOL by Raj

March 23, 2018 | Author: prasad0803 | Category: Database Index, Array Data Structure, Data Type, Software Development, Software Engineering


Comments



Description

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. 5 REC –DATA. The SEARCH statement can be used for single dimension tables only. SET INX-A TO 1 OR SET INX-A UP 1 BY 1. 05 FIELD-2 OCCURS 1 TO 10 TIMES DEPENDING ON FIELD-1. 05 TABLE-ITEM PIC X(8) OCCURS 10 INDEXED BY INX-A. • Binary search: This can be done by using SEARCH ALL statement.Length Tables. I t will be store as a register. • These type of tables will know the length of the tables( or no. Search Table COBOL table can be searched in two ways.. . The compiler calculates the value contained in the index as the occurrence number (subscript) minus 1. This is called sequential search. Syntax SEARCH table-name [VARYING identifier] AT END statements WHEN conditions Statements END-SEARCH.of elements in the table) at run time. Variable . The fifth occurrence of TABLE-ITEM is the binary value contained in index INXA is (5-1) * 8 =32. • The OCCURS DEPENDING ON (ODO) clause can be used to define variablelength tables. • Linear search: This can be done by using SEARCH statement. An index is register. 1 MAIN-AREA. 1 REC-2. 03 REC-1. • • To use SEARCH statement the table must be defined with INDEXED BY phrase along with OCCURS clause. Linear search: The SEARCH statement can be used to perform linear search at the beginning of the index. multiplied by length of table element. 05 FILED-1 PIC 9.• • • • • • • An index can be created by using ‘INDEXED BY’ phrase of OCCURS clause. • Eg. Indexed data-item need not be defined in WORKING-STORAGE SECTION. An Indexed variable can be incremented or decremented or initialized by using SET verb. Algorithm: Binary search will look at first data-item in the table. If AT END is not coded. If entire table has been searched. then the control paused to next line in the program. which satisfied the condition. If match is found Then search is done. This process continues until either a match is found or it is clear that the element is not in the table. We will next look at the middle element of the remaining half and compare it to the element. then we know that we only have to look in first half of the table. we have eliminated half of the table from search. If none of the condition is satisfied. none of WHEN condition is satisfied. then index is increased to next element of the table. then the SEARCH ends and index remains pointed to element of the table. Binary search The SEARCH ALL statement can be used to perform binary search. This will either find a match or eliminate another quarter of the table.. and WHEN conditions are evaluated again. If it is smaller. The conditions in the WHEN phrase are evaluated in the order which they appear. the programmer of a binary search can choose to round or truncate. then AT END statement will be executed. If match is not found.• • • • • • Multiple WHEN conditions can be coded. Based on this decision. If any one of the WHEN conditions satisfied. If it is larger. Note that in establishing what the middle is if the number of elements is even. . then it checks to see the data-item you are trying to find is smaller or larger than middle element. Binary Search is most efficient when you are dealing with large tables. then we know that we only have to look in the second half of the table. . The index need not be necessary to set to 1 prior to starting search. The key field may be element itself or element of the group. The index is always associated with the first index-name in the OCCURS clause.• • • • The Table must be in sorted on some key for the binary search. Can a SEARCH be applied to a table which does not have an INDEX defined? Ans: No. the table must be indexed. 01 WS-TABLE. 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. 5. 01 MULTI-DIM-ARRAY. 05 MDA-FIRST-DIM OCCURS 20. There is no need to set the index value.. 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. 3. Use the SEARCH verb without ALL phrase 6. No dynamic arrays. Use SEARCH ALL verb. To search from beginning. 2.VARYING? Ans: The serial SEARCH verb (SEARCH without ALL) 4. . 07 MDA-SECOND-DIM OCCURS 30 PIC X. . A single-dimension array of thirty one-byte characters. Use OCCURS clause with ASC/DESC KEY IS dataname1 option The table must be indexed. A multi-dimension array of 20 times 30 one-byte characters. set the index value to 1. 03 WS-TABLE-EL OCCURS 5 TIMES. 01 SINGLE-DIM-ARRAY 05 SDA-ITEM PIC X OCCURS 30. The KEY IS phrase identifies the data-item on which the table is sorted. 04 WS-EX REDEFINES FILLER-X PIC X(1). The code will compile and execute as Redefinition of an item subordinate to OCCURS clause. Which SEARCH verb is equivalent to PERFORM.. 04 FILLER-X PIC X(1) VALUE 'A'. Before the use of the SEARCH the index must have some initial value.• For Binary search the OCCURS clause of the table must have key clause in addition to the INDEXED BY clause. Possible Interview Questions on Tables 1. 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. What would be the output of DISPLAY WS-TABLE? Ans: 'AAAAA'. 7. Then DISPLAY DIS-VARIABLE. Can I display the index ?. SET DIS-VARIABLE RO INDEX-VARIBLE. 8. . Ans: Yes.
Copyright © 2024 DOKUMEN.SITE Inc.