COBOL Array Operations

Source: Internet
Author: User

The following is an example of an array:
01 RECORD-X.
03 month occurs 12 times.
05 name pic x (12 ).
05 days occurs 31 Tiems.
07 comment pic x (20 ).
07 TEMP-HI PIC s999.
07 TEMP-LO PIC s999.
77 current-day PIC 99.
The call is as follows:
Month (5): group items that contain the name and days tables.
Name of month (5): Basic alphanumeric item, with a length of 12 characters.
Name (5) () the first three characters of the above 5th items.
TEMP-HI () Basic numeric item with a length of 3 characters. The first parameter is the position in the month array, and the last parameter is the position in the days array. It is equivalent to month [1]. days [31]. Temp-hi.
Comment of RECORD-X (5, current-day) (11) the last 10 characters in the comment field of the current-day of the fifth month of the basic alphanumeric item.

03 text pic xxx occurs 1 to 12 times depending on Ix.
Defines a variable length array. The defined length is 12, which is subject to IX. If IX is 10, the text length is 30.

Index
Index
The index name can be used as a variable of the perform statement for the set and search statements and as a subscript.
03 text pic xxx occurs 7 times indexed by index
05 a pic xxx
05 B PIC xxx
Perform varying index from 1 by 1 until index> 7
Display a (INDEX), B (INDEX)
End-perform.
Note:
77 index PIC 99
03 text pic xxx occurs 7 times
05 a pic xxx
05 B PIC xxx
Perform varying index from 1 by 1 until index> 7
Display a (INDEX), B (INDEX)
End-perform.

Key
Keywords include ascending (ascending, from low to high) and descending (descending, from high to low ).
03 text pic xxx occurs 7 times ascending key is A, B.
05 a pic XXX.
05 B PIC XXX.
Elements in text are sorted in ascending order of A, and elements in the same order of a are sorted in ascending order of B.
The keyword defines the sorting of tables when the search statement contains all phrases.

Table initialization: You can initialize data items individually or together.
03 text pic xxx occurs 7 times values space.
05 a pic XXX.
05 B PIC XXX. Space is initialized together.
03 text pic xxx occurs 7 times.
05 a pic XXX values space.
05 B PIC XXX values space. initialized separately.

Set
Save or restore the index value. Set A to B.
Increment or decrease the index by an integer. Set a up/down by N.

Search
Search a [varying B] At end c {When condition d}... end-Search
A is a table with the indexed by phrase. Each step calculates the condition in turn. If the condition is true, execute D and the search statement ends. If the value is false, the index value increases. When the maximum index is reached, at end C is executed, and the search statement ends.
If the index value is the largest at the beginning, the at end statement is directly executed without the when statement.
Varying specifies index B, which is omitted from index.
The following example shows the next month with 31 days in the month table.
01 month-data.
03 month-info.
05 pic x (10) values "January ".
05 pic x (3) values "Jan ".
05 PIC 99 values 31.
05 pic x (10) values "February ".
05 pic x (3) values "FEB ".
05 PIC 99 values 28.
... (Omitted code)
03 month redefines month-Info occurs 12 times indexed by month-index.
05 name pic x (10 ).
05 abbrev pic x (3 ).
05 days PIC 99.
77 current-month PIC 99.
77 next-31-day-month PIC 99.

Set month-index to current-month.
Set month-index up by 1.
Search month
At end move 1 to month-Index
Search month
At end move 0 to next-31-day-month
When days (month-index) = 31
Set next-31-day-month to month-Index
End-search
When days (month-index) = 31
Set next-31-day-month to month-Index
End-search
To search for a multi-dimensional table, you need to put search in the perform.
01 table-data.
03 dim-1 occurs 10 times indexed by x1.
05 dim-2 occurs 20 times indexed by x2.
07 name pic x (20 ).
07 state PIC xx.
...
Perform varying X1 from 1 by 1 until x1> 20
Set X2 to 1
Search dim-2
When state (x1, x2) = "Al "....
Wneh state (x1, x2) = "az "....
End search
End-perform.
Another kind of search is to search for tables with keywords, which is similar to the search above, mainly because keywords are used as search conditions in the when condition. For example, if the keyword is A, it must have when a = .....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.