Você está na página 1de 5

. What is table in COBOL? How do you define a Table?

In COBOL, youve learnt how to store a simple integer number, real number, textstrings etc. in Computer Memory. A simple variable can hold only one, single dataitem. For example, a PIC X(10) variable can store the text-string HELLOWORLD, a PIC 9(03) variable can store the integer number 125, and so on. However, in your day-to-day life, you often come across a list of items, say your shopping list, your phone-book(list of contacts), your favourite books(list of books) etc. What if, you want to store a list of data-items in Computer Memory? Say, for example like a collection of 5 PIC X(10) text-strings, or 100 PIC 9(03) numbers. Table can be used to store a list of data-items. An table is a list, a collection of similar data-items. So, Tables store multiple data-items of the same type. Take data-items, which are logically-related to each other, and put them together, in a table. For example, a table of integer numbers = {1, 2, 5, 8, -6}. Thus, in essence, a table is a set of data-items of the same type. A table must be assigned a unique name. Say, for example, you can take a table, a set of PIC 9(04) numbers {1000, 2000, 5000, 8000, 6000} and assign it the name INCOME-TABLE. Then INCOME-TABLE = {1000, 2000, 5000, 8000, 6000}. The table INCOME-TABLE is said to have five members 1000, 2000, 5000, 8000 and 6000. These are called elements of the Table. The number of elements in an table is called the size of the table, or table length. Q. How do you refer to individual elements of a table? Consider the table INCOME-TABLE = {1000, 2000, 5000, 8000, 6000}. You access the elements of a table, by specifying the position. The element at the 1st position is 1000, 2nd position is 2000, 3rd position is 5000 and so on... So, if you were to ask a question, Give the element at position 4, the answer would be 8000. Q. Could you demonstrate via an example, how to create a simple table and retrieve data from it? Suppose you want to store the names of you 5 friends in a COBOL Table. To create a table in COBOL, you must understand 3 important things Q1. Assigning a unique name to the table A. FRIENDS-TABLE is the unique name of the table Q2. What is contains? A. The data the name of information the data being recorded in this table, and how many data-items it generally stored in this table represents your friends information. So the record should be FRIEND-RECORD. The FRIEND-RECORD should store about 5 friends. So, it contains 5 cells.

3. What is the specification/PICTURE clause for each such data-item? Each such cell contains FRIEND-NAME, which is textual-string and can be 20 characters long. In COBOL, you would write 01 FRIENDS-TABLE 02 FRIEND-RECORD OCCURS 5 TIMES 03 FRIEND-NAME PIC X(20)

Understand that the OCCURS Clause is used to specify, how many items(cells) are contained in the record.

Now, you can access the elements of this table as follows : To access your first friends name, you write FRIEND-NAME(1) To access your second friends name,you write FRIEND-NAME(2) ... To access your fifth friends name, you write FRIEND-NAME(5) The COBOL Definition of the above friends table, would look like this :

Given below is a simple COBOL Program, that shows how to store data in the Friends table, and display the contents of the table.

Upon running the above COBOL Program, you should get the following output -

Note: Since, FRIEND-RECORD is a group consisting of only one sub-item, you can use FRIEND-RECORD and FRIEND-NAME interchangeably. Which means, FRIEND-RECORD(1) and FRIEND-NAME(1), both would work. Q. Can the elements of the table be more complex group-items? The elements of a table can be simple data-items like a text-string PIC X(10). On the other hand, they may also items, like 01 EMPLOYEE item, which in turn consists of text-string and 02 SALARY PIC 9(04) number. So, you can numbers, 100 text-strings or 3 employees. number PIC 9(04), or a be complex group-dataa 02 EMP-NAME PIC X(20) have a table of 50

In the example, given here, I have created a table, that stores the data of 3 employees. The temporary variables defined in the Working Storage Section of the COBOL Program.

Now, the table EMPLOYEE-TABLE can contain the data of 3 employees. Now, you can write a COBOL Program, to store some data in the Employee Table, and then display its contents. Look at the sample COBOL Program, given below.

Upon running the above COBOL Program, you get the following output -

Q. What are two-dimensional tables? How do you create them? A table is a collection/list of data-items. Each data-item can be a simple dataitem, or a complex group-item. What if the data-item is a table? Thus, you have a Table of tables. A 2-dimensional table looks like a matrix of cells, consisting of rows and columns. To access any cell at the intersection of a row and column, you need to know the row-no and the column-no. Suppose, we wish to setup a table, which will represent the below statistic. The table below gives the class-wise and year-wise breakup of the spending capacity of an Indian Household. Spending of an Average Indian Household annually in Rs.

First let me show, how to create Working-Storage definition for this table in COBOL. You need to create a 5 x 4 table(5 rows, each having 4 cells) 1. You need to assign a unique name to the table, so say the name of the table is AVERAGE-INDIAN-SPEND-TABLE. 01 AVERAGE-INDIAN-SPEND-TABLE.

2. Next, we have collected statistics for 5 different classes. So, the table will contain 5 records one for each class. 01 AVERAGE-INDIAN-SPEND-TABLE 02 SOCIAL-CLASS OCCURS 5 TIMES

3. Next, each of the classes, Upper, Upper-Middle, Middle-Middle, Lower-Middle and Lower class give the year-wise breakup of the money spend for 4 years (2007,2008,2009,2010). So, each row contains 4 cells. 01 AVERAGE-INDIAN-SPEND-TABLE 02 SOCIAL-CLASS 5 TIMES 03 YEAR OCCURS 4 TIMES

4. Moreover, the actual data in each cell is the money spend in Rupees. So, in general, each cell has the format PIC 9(4)V99. 01 AVERAGE-INDIAN-SPEND-TABLE 02 SOCIAL-CLASS OCCURS 5 TIMES

03 YEAR OCCURS 4 TIMES 04 AMOUNT PIC 9(5)V99 In COBOL, you can in-fact go a level further, and create 3-dimensional cubes, 4,5,.. upto 7-dimensional tables as per COBOL-85 Standard. Q. Can you suggest any general formula for creating two-dimensional tables in COBOL? Consider the following working-storage definition of a table 02 A OCCURS r TIMES 03 B OCCURS c TIMES 04 CELL PIC p (p = field size) In general, the above formula holds to create a table of size r rows x c columns, where each cell has PICTURE Clause PIC P. The PICTURE Clause is used at the lowest hierarchical level of a table. Q. Could you show me a complete example of how to store data and retrieve data from 2-dimensional tables? Lets take the example of the AVERAGE-INDIAN-SPEND-TABLE. The COBOL Definition of this table is shown below -

The below COBOL Program, shows how to fill up AVERAGE-INDIAN-SPEND-TABLE with values. You can refer to any cell of this 2-dimensional table by using (row,col). For example, to refer to the amount in 2nd row, 4th column, you would write AMOUNT(2,4). The COBOL Program is divided into 2 parts :1) Filling(Initialising) the Table 2) Displaying the contents of the Table

The next COBOL Paragraph displays values in the AVERAGE-INDIAN-SPEND-TABLE on the screen.

Upon running the above COBOL Program, you get the contents of the table displayed on the screen.

Você também pode gostar