Compaq COBOL
User Manual


Previous Contents Index

3.6 Using the MOVE Statement

The MOVE statement moves the contents of one item into another. For example:


MOVE FIELD1 TO FIELD2 
 
MOVE CORRESPONDING FIELD1 TO FIELD2 

FIELD1 is the sending item name, and FIELD2 is the receiving item name.

The first statement causes the compiler to move the contents of FIELD1 into FIELD2. The two items need not be the same size, class, or usage; they can be either group or elementary items. If the two items are not the same length, the compiler aligns them on one end or the other. It also truncates or space-fills the other end. The movement of group items and nonnumeric elementary items is discussed in Section 3.6.1 and Section 3.6.2, respectively.

The MOVE statement alters the contents of every character position in the receiving item.

3.6.1 Group Moves

If either the sending or receiving item is a group item, the compiler considers the move to be a group move. It treats both the sending and receiving items as if they were alphanumeric items.

If the sending item is a group item, and the receiving item is an elementary item, the compiler ignores the receiving item description except for the size description, in bytes, and any JUSTIFIED clause. It conducts no conversion or editing on the sending item's data.

3.6.2 Elementary Moves

If both items of a MOVE statement are elementary items, their PICTURE character-strings control their data movement. If the receiving item was described as numeric or numeric edited, the rules for numeric moves control the data movement (see Section 2.6). Nonnumeric receiving items are alphanumeric, alphanumeric edited, or alphabetic.

Table 3-2 shows the valid and invalid nonnumeric elementary moves.

Table 3-2 Nonnumeric Elementary Moves
  Receiving Item Category
Sending Item Category   Alphanumeric
  Alphabetic Alphanumeric Edited
ALPHABETIC Valid Valid
ALPHANUMERIC Valid Valid
ALPHANUMERIC EDITED Valid Valid
NUMERIC INTEGER
(DISPLAY ONLY)
Invalid Valid
NUMERIC EDITED Invalid Valid

In all valid moves, the compiler treats the sending item as though it had been described as PIC X(n). A JUSTIFIED clause in the sending item's description has no effect on the move. If the sending item's PICTURE character-string contains editing characters, the compiler uses them only to determine the item's size.

In valid nonnumeric elementary moves, the receiving item controls the movement of data. All of the following characteristics of the receiving item affect the move:

The JUSTIFIED clause and editing characters are mutually exclusive.

When an item that contains no editing characters or JUSTIFIED clause in its description is used as the receiving item of a nonnumeric elementary MOVE statement, the compiler moves the characters starting at the leftmost position in the item and proceeding, character by character, to the rightmost position. If the sending item is shorter than the receiving item, the compiler fills the remaining character positions with spaces. If the sending item is longer than the receiving item, truncation occurs on the right.

Numeric items used in nonnumeric elementary moves must be integers in DISPLAY format.

If the description of the numeric data item indicates the presence of an operational sign (either as a character or an overpunched character), or if there are P characters in its character-string, the compiler first moves the item to a temporary location. It removes the sign and fills out any P character positions with zero digits. It then uses the temporary value as the sending item as if it had been described as PIC X(n). The temporary value can be shorter than the original value if a separate sign was removed, or longer than the original value if P character positions were filled with zeros.

If the sending item is an unsigned numeric class item with no P characters in its character-string, the MOVE is accomplished directly from the sending item, and a temporary item is not required.

If the numeric sending item is shorter than the receiving item, the compiler fills the receiving item with spaces.

3.6.2.1 Edited Moves

This section explains the following insertion editing characters:
B Blank insertion position
0 Zero insertion position
/ Slash insertion position

When an item with an insertion editing character in its PICTURE character-string is the receiving item of a nonnumeric elementary MOVE statement, each receiving character position corresponding to an editing character receives the insertion byte value. Table 3-3 illustrates the use of such symbols with the following statement, where FIELD1 is described as PIC X(7):


MOVE FIELD1 TO FIELD2 

Table 3-3 Data Movement with Editing Symbols
FIELD1 FIELD2
  Character-String Contents After MOVE
070476 XX/99/XX 07/04/76
04JUL76 99BAAAB99 04sJULs76
2351212 XXXBXXXX/XX/ 235s1212/ss/
123456 0XB0XB0XB0X 01s02s03s04


Legend: s = space

Data movement always begins at the left end of the sending item and moves only to the byte positions described as A, 9, or X in the receiving item PICTURE character-string. When the sending item is exhausted, the compiler supplies space characters to fill any remaining character positions (not insertion positions) in the receiving item. If the receiving item is exhausted before the last character is moved from the sending item, the compiler ignores the remaining sending item characters.

Any necessary conversion of data from one form of internal representation to another takes place during valid elementary moves, along with any editing specified for, or de-editing implied by, the receiving data item.

3.6.2.2 Justified Moves

A JUSTIFIED RIGHT clause in the receiving item's data description causes the compiler to reverse its usual data movement conventions. It starts with the rightmost characters of both items and proceeds from right to left. If the sending item is shorter than the receiving item, the compiler fills the remaining leftmost character positions with spaces. If the sending item is longer than the receiving item, truncation occurs on the left. Table 3-4 illustrates various PICTURE character-string situations for the following statement:


MOVE FIELD1 TO FIELD2 

Table 3-4 Data Movement with the JUSTIFIED Clause
FIELD1 FIELD2
PICTURE
Character-String
Contents PICTURE
Character-String
(and JUST-Clause)
Contents After
MOVE
    XX AB
    XXXXX ABCss
XXX ABC XX JUST BC
    XXXXX JUST ssABC


Legend: s = space

3.6.3 Multiple Receiving Items

If you write a MOVE statement containing more than one receiving item, the compiler moves the same sending item value to each of the receiving items. It has essentially the same effect as a series of separate MOVE statements, all with the same sending item.

The receiving items need have no relationship to each other. The compiler checks the validity of each one independently and performs an independent move operation on each one.

Multiple receiving items on MOVE statements provide a convenient way to set many items equal to the same value, such as during initialization code at the beginning of a section of processing. For example:


MOVE SPACES TO LIST-LINE, EXCEPTION-LINE, NAME-FLD. 
 
MOVE ZEROS TO EOL-FLAG, EXCEPT-FLAG, NAME-FLAG. 
 
MOVE 1 TO COUNT-1, CHAR-PTR, CURSOR. 

3.6.4 Subscripted Moves

Any item (other than a data item that is not subordinate to an OCCURS clause) of a MOVE statement can be subscripted, and the referenced item can be used to subscript another name in the same statement.

For example, when more than one receiving item is named in the same MOVE statement, the order in which the compiler evaluates the subscripts affects the results of the move. Consider the following examples:


MOVE FIELD1(FIELD2) TO FIELD2 FIELD3. 

In this example, the compiler evaluates FIELD1(FIELD2) only once, before it moves any data to the receiving items. It is as if the single MOVE statement were replaced with the following three statements:


MOVE FIELD1(FIELD2) TO TEMP. 
 
MOVE TEMP TO FIELD2. 
 
MOVE TEMP TO FIELD3. 

In the following example, the compiler evaluates FIELD3(FIELD2) immediately before moving the data into it, but after moving the data from FIELD1 to FIELD2:


MOVE FIELD1 TO FIELD2 FIELD3(FIELD2). 

Thus, it uses the newly stored value of FIELD2 as the subscript value. It is as if the single MOVE statement were replaced with the following two statements:


MOVE FIELD1 TO FIELD2. 
 
MOVE FIELD1 TO FIELD3(FIELD2). 

3.6.5 Common Nonnumeric Item MOVE Statement Errors

The compiler considers any MOVE statement that contains a group item (whether sending or receiving) to be a group move. If an elementary item contains editing characters or a numeric integer, these attributes of the receiving item have no effect on the action of a group move.

3.6.6 Using the MOVE CORRESPONDING Statement for Nonnumeric Items

The MOVE CORRESPONDING statement allows you to move multiple items from one group item to another group item, using a single MOVE statement. See the Compaq COBOL Reference Manual for rules concerning the CORRESPONDING phrase. When you use the CORRESPONDING phrase, the compiler performs an independent move operation on each pair of corresponding items from the operands and checks the validity of each. Example 3-2 shows the use of the MOVE CORRESPONDING statement.

Example 3-2 Sample Record Description Using the MOVE CORRESPONDING Statement

01 A-GROUP.                       01 B-GROUP. 
   02 FIELD1.                        02 FIELD1. 
      03 A PIC X.                       03 A PIC X. 
      03 B PIC 9.                       03 C PIC XX. 
      03 C PIC XX.                      03 E PIC XXX. 
      03 D PIC 99. 
      03 E PIC XXX. 
 
    MOVE CORRESPONDING 
         A-GROUP TO B-GROUP. 
 
 
Equivalent MOVE statements: 
 
MOVE A OF A-GROUP TO A OF B-GROUP. 
 
MOVE C OF A-GROUP TO C OF B-GROUP. 
 
MOVE E OF A-GROUP TO E OF B-GROUP. 

3.6.7 Using Reference Modification

You can use reference modification to define a subset of a data item by specifying its leftmost character position and length. Reference modification is valid anywhere an alphanumeric identifier is allowed unless specific rules for a general format prohibit it. The following is an example of reference modification:


WORKING-STORAGE SECTION. 
01  ITEMA  PIC X(10)  VALUE IS "XYZABCDEFG". 
        . 
        . 
        . 
    MOVE ITEMA(4:3) TO... 
 
 
 
 
 
IDENTIFIER                 VALUE 
 
ITEMA (4:3)                ABC 

For more information on reference modification rules, refer to the Compaq COBOL Reference Manual.


Chapter 4
Handling Tables

A table is one or more repetitions of one element, composed of one or more data items, stored in contiguous memory locations.

In this chapter you will find:

4.1 Defining Tables

You define a table by using an OCCURS clause following a data description entry. The literal integer value you specify in the OCCURS clause determines the number of repetitions, or occurrences, of the data description entry, thus creating a table. Compaq COBOL allows you to define from 1- to 48-dimension tables.

After you have defined a table, you can load it with data. One way to load a table is to use the INITIALIZE statement or the VALUE clause to assign values to the table when you define it (see Figure 4-10).

To access data stored in tables, use subscripted or indexed procedural instructions. In either case, you can directly access a known table element occurrence or search for an occurrence based on some known condition.

You can define either fixed-length tables or variable-length tables, and they may be single or multidimensional. The following sections describe how to use the OCCURS clause and its options. For more information on tables and subscripting, see the Compaq COBOL Reference Manual.

4.1.1 Defining Fixed-Length, One-Dimensional Tables

To define fixed-length tables, use Format 1 of the OCCURS clause (refer to the Compaq COBOL Reference Manual). This format is useful when you are storing large amounts of stable or frequently used reference data. Options allow you to define single or multiple keys, or indexes, or both.

A definition of a one-dimensional table is shown in Example 4-1. The integer 2 in the OCCURS 2 TIMES clause determines the number of element repetitions. For the table to have any real meaning, this integer must be equal to or greater than 2.

Example 4-1 One-Dimensional Table

01  TABLE-A. 
    05  ITEM-B PIC X OCCURS 2 TIMES. 

The organization of TABLE-A is shown in Figure 4-1.

Figure 4-1 Organization of the One-Dimensional Table


Example 4-1 specifies only a single data item. However, you can specify as many data items as you need in the table. Multiple data items are shown in Example 4-2.

Example 4-2 Multiple Data Items in a One-Dimensional Table

01  TABLE-A. 
    05  GROUP-B OCCURS 2 TIMES. 
        10  ITEMC PIC X. 
        10  ITEMD PIC X. 

The organization of this table is shown in Figure 4-2.

Figure 4-2 Organization of Multiple Data Items in a One-Dimensional Table


Example 4-1 and Example 4-2 both do not use the KEY IS or INDEXED BY optional phrases. The INDEXED BY phrase implicitly defines an index name. This phrase must be used if any Procedure Division statements contain indexed references to the data name that contains the OCCURS clause. The KEY IS phrase means that repeated data is arranged in ascending or descending order according to the values in the data items that contain the OCCURS clause. (The KEY IS phrase does not cause the data in the table to be placed in ascending or descending order; rather, it allows you to state how you have arranged the data.) For further information about these OCCURS clause options, see the Compaq COBOL Reference Manual.

If you use either the SEARCH or the SEARCH ALL statement, you must specify at least one index. The SEARCH ALL statement also requires that you specify at least one key. Specify the search key using the ASCENDING/DESCENDING KEY IS phrase. (See Section 4.3.8 for information about the SEARCH statement and Section 4.3.4 for information about indexing.) When you use the INDEXED BY phrase, the index is internally defined and cannot be defined elsewhere. Example 4-3 defines a table with an ascending search key and an index.

Example 4-3 Defining a Table with an Index and an Ascending Search Key

01  TABLE-A. 
    05  ELEMENTB OCCURS 5 TIMES 
                 ASCENDING KEY IS ITEMC 
                 INDEXED BY INDX1. 
        10  ITEMC PIC X. 
        10  ITEMD PIC X. 

The organization of this table is shown in Figure 4-3.

Figure 4-3 Organization of a Table with an Index and an Ascending Search Key


4.1.2 Defining Fixed-Length, Multidimensional Tables

Compaq COBOL allows 48 levels of OCCURS nesting. If you want to define a two-dimensional table, you define another one-dimensional table within each element of the one-dimensional table. To define a three-dimensional table, you define another one-dimensional table within each element of the two-dimensional table, and so on.

A two-dimensional table is shown in Example 4-4.

Example 4-4 Defining a Two-Dimensional Table

01  2D-TABLE-X. 
    05  LAYER-Y OCCURS 2 TIMES. 
        10  LAYER-Z OCCURS 2 TIMES. 
            15  CELLA PIC X. 
            15  CELLB PIC X. 

The organization of this two-dimensional table is shown in Figure 4-4.

Figure 4-4 Organization of a Two-Dimensional Table


Example 4-5 shows a three-dimensional table.

Example 4-5 Defining a Three-Dimensional Table

01 TABLE-A. 
    05  LAYER-B OCCURS 2 TIMES. 
        10  ITEMC PIC X. 
        10  ITEMD PIC X OCCURS 3 TIMES. 
        10  ITEME OCCURS 2 TIMES. 
            15  CELLF PIC X. 
            15  CELLG PIC X OCCURS 3 TIMES. 

The organization of this three-dimensional table is shown in Figure 4-5.

Figure 4-5 Organization of a Three-Dimensional Table


4.1.3 Defining Variable-Length Tables

To define a variable-length table, use Format 2 of the OCCURS clause (refer to the Compaq COBOL Reference Manual). Options allow you to define single or multiple keys, or indexes, or both.

Example 4-6 illustrates how to define a variable-length table.

It uses from two to four occurrences depending on the integer value assigned to NUM-ELEM. You specify the table's minimum and maximum size with the OCCURS (minimum size) TO (maximum size) clause. The minimum size value must be equal to or greater than zero and the maximum size value must be greater than the minimum size value. The DEPENDING ON clause is also required when you use the TO clause.

The data-name of an elementary, unsigned integer data item is specified in the DEPENDING ON clause. Its value specifies the current number of occurrences. The data-name in the DEPENDING ON clause must be within the minimum to maximum range.

Unlike fixed-length tables, you can dynamically alter the number of element occurrences in variable-length tables.

By generating the variable-length table in Example 4-6, you are, in effect, saying: "Build a table that can contain at least two occurrences, but no more than four occurrences, and set its present number of occurrences equal to the value specified by NUM-ELEM."

Example 4-6 Defining a Variable-Length Table

01  NUM-ELEM PIC 9. 
       . 
       . 
       . 
01  VAR-LEN-TABLE. 
    05  TAB-ELEM OCCURS 2 TO 4 TIMES DEPENDING ON NUM-ELEM. 
        10 A PIC X. 
        10 B PIC X. 


Previous Next Contents Index