Sunday 22 September 2013

Master ARRAY In COBOL In Just A Few Minutes!

ARRAY In COBOL
ARRAY In COBOL

In this tutorial, you will learn ARRAY processing. Array processing is a critical part of any programming language and they are widely used by the programmer to perform various data operations.

An ARRAY in COBOL is used for various data manipulation. For example, a tax table may be used to look up the amount of income tax to be withheld from payroll checks. You can read a file into an array in COBOL or search in COBOL.

The easiest way to work with COBOL array/table is to use subscripts or Index in COBOL. Array in COBOL is also known as the Table in COBOL, these terms are used interchangeably in the tutorial. So, let's start with the array tutorial.


What is an ARRAY in COBOL?


An Array in COBOL or Table in COBOL is a linear data structure that is used to store, access and process data easily and efficiently. It is a collection of homogenous data items (Data items that possess the same size and structure) that can be referred by a single name.

The data items contained in a COBOL Table (i.e. array in COBOL) are called its elements and can be used in arithmetic and logical operations. The elements of a table are stored contiguously in the memory. The elements of a Table can be either elementary items or group items and further a Table can be either of fixed length or variable length.

In COBOL, an array/table can have dimensions up to 7. Though an array/table is a very simple data structure, it is a very powerful tool available to the programmer. It can save many lines of code and simplify the programming logic to a considerable extent.

COBOL has a wealth of logical control structures and statements that allow the programmer to exploit the complete range of Table handling capabilities.

What are subscripts COBOL?


A subscript is a field that contains an occurrence number, which indicates the occurrence of the field you want to refer to. A subscript must be defined as an integer, and it should be defined with computational usage for efficiency.

You can use relative subscripts to increase or decrease a subscript value by a literal value when you refer to a table entry.
You can use the same subscript to refer to entries in two or more arrays/tables in COBOL, and you can use two or more subscripts to refer to the entries in a single COBOL array/ COBOL table.

Note: The COBOL array index starts from 1 (i.e. One). However, in other programming languages such as C, C++, Python its starts from 0 (i.e. Zero).

What is Index in COBOL?


An index represents a displacement value from the start of the table. Due to this fact, indexes are more efficient to use than subscripts, which have to be converted to displacement values.

You define a table that uses an index the same way you define a table that uses a subscript except that you include the Indexed By clause. This clause names one or more indexes that will be used to refer to the table entries. The indexes you name are defined automatically.

How to Define the index in COBOL?


01 TEMP-TABLE         VALUE ZERO.
   05 DAY-GROUP       OCCURS 16 TIMES
                      INDEXED BY PRICE-TABLE-INDEX.
     10 TEMP-DAY      PIC 9(03).
     10 TEMP-IN-DGR   PIC S99V99.
*

How to define an Array in COBOL (i.e. table in COBOL)?


Similar to any other data item, an Array in COBOL is defined in the DATA DIVISION section of a COBOL program. To specify the repeated occurrence of data items with the same format, the OCCURS clause is used. The OCCURS clause specifies the maximum number of elements that can be stored in the COBOL Table/array.

The syntax of a table definition.


level-number data-name OCCURS integer TIMES

Note: It is important to note that the OCCURS clause can be used with only level numbers 02 to 49. It cannot be used with level number 01, for, it must be used to define fields and not records.

Example 1: One-dimensional Array in COBOL.


     DATA DIVISION.
    *
     WORKING-STORAGE SECTION.
    *
     01 WS-DAILY-TEMPERATURE.
        05 WS-HOURLY-TEMPERATURE PIC 9(2)V9 OCCURS 24 TIMES.

Example 2: Two-dimensional in COBOL.


     DATA DIVISION.
    *
     WORKING-STORAGE SECTION.
    *
     01 WS-TEMPERATURE.
         05 WS-DAYS       OCCURSTIMES.
           10 WS-HOURS    OCCURS 24 TIMES.
              15 WS-TEMP  PIC 9(2)V9.

Example 3: (Three) 3 dimensional array in COBOL.

     DATA DIVISION.
   *
     WORKING-STORAGE SECTION.
   *
     01 WS-TEMPERATURE.
        05 WS-DAYS            OCCURS  7 TIMES.
           10 WS-HOURS        OCCURS 24 TIMES.
                                  15 WS-MINUTES   OCCURS 60 TIMES.
                 25 WS-TEMP   PIC 9(2)V9.

Dynamic array in COBOL with example.


Array in COBOL (i.e. Tables in COBOL) can also have a variable number of elements. This does not imply that the size of the table is dynamically adjusted depending on the number of elements. The variable size is useful to avoid out of range accesses. For example, if a table has been declared to have between 10 and 200 entries and it currently has 30 entries, access to the 55th element is illegal.

 Example 4: Dynamic array in COBOL example.


      01 WS-EMP-TABLE.
         05 WS-NUMBER-OF-EMPLOYEES PIC 9(6).
         05 WS-EMP-REC   OCCURS 1 TO 500 
                         TIMES DEPENDING ON WS-NUMBER-OF-EMPLOYEES.
            10 WS-EMP-NAME PIC A(30).
            10 WS-EMP-DEPT PIC X(04).

Accessing the elements of an array in COBOL (i.e. Table handling in COBOL).


The elements of a table/array can be accessed with the data-item that is lowest in the hierarchy with either a subscript or an index. The subscript/index must be enclosed within a pair of parentheses.

If the elements are to be accessed using the INDEXED BY phrase, then it should be declared in the DATA DIVISION using the INDEXED BY clause as shown in the following examples.

Example 1: Two-dimensional Table defined using INDEXED BY clause (i.e. usage is index in COBOL)


      DATA DIVISION.
  *
   WORKING-STORAGE SECTION.
  *
   01 WS-TEMPERATURE.
      05 WS-DAYS        OCCURS  7 TIMES INDEXED BY I.
         10 WS-HOURS    OCCURS 24 TIMES INDEXED BY J.
            15 WS-TEMP  PIC 9(02)V9.

Example 2: Three-dimensional Table defined using INDEXED BY clause (i.e. usage is index in COBOL)


      DATA DIVISION.
     *
      WORKING-STORAGE SECTION.

      01 WS-TEMPERATURE.
         05 WS-MONTHS        OCCURS 12 TIMES INDEXED BY I.
            10 WS-DAYS       OCCURS 31 TIMES INDEXED BY J.
               15 WS-HOURS   OCCURS 24 TIMES INDEXED BY K.
                  20 WS-TEMP PIC 9(02)V9.

How to initialize an Array in COBOL?


In certain cases, you might need to initialize the values in a table before you use the table. However, the best practice is to initialize the COBOL array before using it in the program. To do that, you can include Value clauses in the table definition as shown in the example below. Here, the VALUE CLAUSE is coded on the elementary items in the table/array. Then, each occurrence of those items is initialized with the indicated value, in this case, zero.

Although you can initialize each field in a table separately, you can also initialize the entire table at once by coding a Value clause at the table level or at the group level (the level that contains the Occurs clause). However, that works only if all of the fields in the table are defined with the same usage and you want to initialize each field to the same value. Otherwise, you’ll want to define each field separately.

Example 1: One-dimensional table initialized using the VALUE clause.


    DATA DIVISION.
   *
    WORKING-STORAGE SECTION.
   *
     01 WS-MONTHS   VALUE “JANFEBMARAPRJUNJULAUGSEPOCTNOVDEC”.
        05 WS-MONTH OCCURS 12 TIMES PIC A(03).

Subscript in COBOL with an example:


The easiest way to work with tables is to use subscripts. To refer to a specific table entry using subscripts, you indicate the occurrence number of the entry. The first entry in a table has an occurrence number of one, the second entry has an occurrence number of two, and so on. The subscript can be a positive integer or a numeric data item or an integer type data item + or – an integer or another integer type data item. If a Table with name WS-EMP-TABLE has 1000 elements, then they are WS-EMP-TABLE(1), WS-EMP-TABLE(2), . . . , WS-EMP-TABLE(1000).

Then the largest value that the subscript can take is the integral value specified in the OCCURS clause. To deal with multi-dimensional arrays, there is a flavor of a PERFORM statement that can be used.

The definition of a subscript.

WORKING-STORAGE SECTION.
*
* Define a subscript.
  05 YR-MNTH-SUB    PIC S99   COMP. 
*
PROCEDURE DIVISION.
*
*Access data from an array using subscript in COBOL.
     MOVE WS-MONTH(YR-MNTH-SUB) To HDR-DTE-MNTH

The index in COBOL with an example:


Although subscripts are easy to use, they’re not as efficient as using indexes. That’s because an index represents a displacement value that points directly to the location of a table entry.

The index in COBOL syntax. 


level-number data-name OCCURS integer TIMES
             INDEXED BY {index-name-1} ...

How to code the SET statement.


Because an index contains a displacement value and not an occurrence number, you can’t use the standard COBOL statements to assign a value to an index or to change the value of an index. Instead, you have to use the Set statement to do that. 

Though the value of a subscript can be modified using MOVE and ARITHMETIC statements, the value of an index can be modified using only the SET statement. There are two basic formats of the SET statement.


SET index name 1 [, index name 2] . . . TO integer

Examples:

      1. SET I TO 4.
      2. SET I, J TO K. Here I, J and K are all indexes.
      3. SET index name 1 [index - name - 2 UP BY integer

Subscript in COBOL Vs Index in COBOL.


Subscript in COBOL: Represents an occurrence of a Table element. Is defined explicitly in the WORKING-STORAGE SECTION. To change the value of subscript, use a PERFORM . . . VARYING or a MOVE statement or an Arithmetic statement for example:

MOVE WS-HOURLY-TEMPERATURE (HRS-SUB) TO HDG3-TEMP-RNGE.

The index in COBOL: Represents the displacement from the address of the first element in the Table. An index is a special subscript created and maintained by the operating system. To change the value of subscript, use a PERFORM . . . VARYING or a SET statement.

Array in COBOL Coding rules.


To refer to an entry in a one-level table, you code the name of the field you want to refer to followed by a space and the subscript name, an occurrence number, or a relative subscript enclosed in parentheses.

If you code a relative subscript, the plus or minus sign must be preceded and followed by a space.


Summary.


A quick recap of the array in the COBOL tutorial. 
  1. You define an array in COBOL using an Occur clause. You can use either indexes or subscripts. An index represents a displacement value, and a subscript represents an occurrence number that must be converted to a displacement value. 
  2. You can use the initialized clause to initialize the table before using the table in program logic.  
  3. To load data in the table, you use a Perform Varying statement that varies the value of the subscript or index for the table. To search a table, you can use a Perform Varying statement for each level of the table.
  4. When you define a table with indexes, you can use the Search statement to perform a sequential search on the table. If the entries are in sequence by a key field, you can also use the Search All statement to perform a binary search.
  5. You use the Set statement to work with indexes in COBOL or index data items. You can use this statement to assign an index value or to increase or decrease an index value.

Created with Artisteer

7 comments:

  1. Whats up! I just want to give a huge thumbs up for the great information you have got right here
    on this post. I will likely be coming again to your weblog for more soon.


    Stop by my web blog :: Holika Holika

    ReplyDelete
  2. Simply desire to say your article is as surprising.
    The clarity in your post is just great and i can assume you are an expert on this subject.
    Well with your permission allow me to grab your RSS feed
    to keep up to date with forthcoming post. Thanks a million and
    please continue the rewarding work.

    Feel free to surf to my homepage :: die cast metal

    ReplyDelete
  3. Good day! I just want to give a huge thumbs up for the great information
    you have got right here on this post. I will be coming again to your weblog for extra soon.


    my blog post - 廣告 ()

    ReplyDelete
  4. Excellent happy analytical vision for the purpose of details and
    can anticipate complications just before these people take place.


    My weblog ... rocker switch (jacquiedanielson.newsvine.com)

    ReplyDelete
  5. Howdy! I simply wish to give an enormous thumbs up for the great info you have got here on this post.

    I will likely be coming again to your weblog for extra soon.



    My blog; 情趣用品, ,

    ReplyDelete
  6. I like to disseminate knowledge that I have built up with the season to assist improve group functionality.


    My blog baby girl strollers

    ReplyDelete
  7. Hi there, I discovered your web site by way of Google whilst looking
    for a similar topic, your site came up, it appears
    to be like great. I have bookmarked it in my google bookmarks.

    Hi there, simply was alert to your blog thru Google, and located that
    it's truly informative. I'm gonna watch out for brussels.

    I'll appreciate if you continue this in future. A lot of other folks can be benefited out of your writing.
    Cheers!

    Here is my webpage - tampa luxury apartments

    ReplyDelete

New In-feed ads