[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
Reference Manual


Previous Contents Index


INITIALIZE-SEARCH.
    MOVE 1 TO CUSTOMER-REGION.
    MOVE "NH" TO CUSTOMER-USPS-STATE.

    DISPLAY "States in customer's region:".

SEARCH-LOOP.
     SEARCH STATES
        AT END
                 GO TO SEARCH-END
        WHEN STATE-USPS-CODE (STATE-INDEX) = CUSTOMER-USPS-STATE
                 SET STATE-NUM TO STATE-INDEX
        WHEN STATE-REGION (STATE-INDEX) = CUSTOMER-REGION
                 DISPLAY STATE-USPS-CODE (STATE-INDEX)
                         " " WITH NO ADVANCING.
    SET STATE-INDEX UP BY 1.
    GO TO SEARCH-LOOP.

SEARCH-END.
    DISPLAY " "
    DISPLAY "Customer state index number = " STATE-NUM.

The following lists the results of the serial search with two WHEN phrases:


States in customer's region:
CT DC DE MA MD ME NJ NY PA RI VT

Customer state index number = 31
  • Updating a table in a SEARCH statement:


    GET-NAME.
        DISPLAY "Enter name: " NO ADVANCING.
        ACCEPT CUSTOMER-NAME.
        SET NAME-INDEX TO 1.
        SEARCH NAME-ENTRY
          AT END
            DISPLAY "   Table full"
            SET NAME-INDEX TO 1
            PERFORM SHOW-TABLE 8 TIMES
            STOP RUN
          WHEN LAST-NAME (NAME-INDEX) = CUSTOMER-NAME
            ADD 1 TO NAME-COUNT (NAME-INDEX)
          WHEN LAST-NAME (NAME-INDEX) = SPACES
            MOVE CUSTOMER-NAME TO LAST-NAME (NAME-INDEX)
            MOVE 1 TO NAME-COUNT (NAME-INDEX).
        GO TO GET-NAME.
    SHOW-TABLE.
        DISPLAY LAST-NAME (NAME-INDEX) " " NAME-COUNT (NAME-INDEX).
        SET NAME-INDEX UP BY 1.
    

    The following lists the results of updating a table in a SEARCH statement:
    Enter name: CRONKITE
    Enter name: GEORGE
    Enter name: PHARES
    Enter name: CRONKITE
    Enter name: BELL
    Enter name: SMITH
    Enter name: FRANKLIN
    Enter name: HENRY
    Enter name: GEORGE
    Enter name: ROBBINS
    Enter name: BELL
    Enter name: FRANKLIN
    Enter name: SMITH
    Enter name: BELL
    Enter name: SMITH
    Table full
    CRONKITE 002
    GEORGE 002
    PHARES 001
    BELL 003
    SMITH 003
    FRANKLIN 002
    HENRY 001
    ROBBINS 001

    6.8.32 SET

    Function

    The SET statement sets values of indexes associated with table elements. It can also change the value of a conditional variable, change the status of an external switch, and store the address of a COBOL identifier reference at run time.


    rsult

    is an index-name, the identifier of an index data item, or an elementary numeric data item described as an integer.

    val

    is a positive integer, which can be signed. It can also be an index-name (or the identifier of an index data item) or an elementary numeric data item described as an integer.

    indx

    is an index-name.

    increm

    is an integer, which can be signed. It can also be the identifier of an elementary numeric data item described as an integer.

    cond-name

    is a condition-name that must be associated with a conditional variable.

    switch-name

    is the name of an external switch defined in the SPECIAL-NAMES paragraph.

    pointer-id

    is a data-name whose data description entry must contain the USAGE IS POINTER clause.

    identifier

    is a data item in the File, Working-Storage, Linkage, or Subschema Section.

    status-code-id

    is a word or longword integer data item represented by PIC S9(1) to S9(9) COMP or PIC 9(1) to 9(9) COMP.

    Syntax Rule

    No two occurrences of cond-name can refer to the same conditional variable.

    General Rules

    Formats 1 and 2

    1. Index-names are associated with a table in the table's OCCURS clause INDEXED BY phrase.
    2. If rsult is an index-name, its value after SET statement execution must correspond to an occurrence number of an element in the associated table.
    3. If val is an index-name, its value before SET statement execution must correspond to an occurrence number of an element in the table associated with rsult.
    4. The value of indx, both before and after SET statement execution, must correspond to an occurrence number of an element in the table associated with indx.

    Format 1

    1. The SET statement sets the value of rsult to refer to the table element whose occurrence number corresponds to the table element referred to by val. If val is an index data item, no conversion occurs.
    2. If rsult is an index data item, val cannot be an integer. No conversion occurs when rsult is set to the value of val.
    3. If rsult is not an index data item or an index-name, val can only be an index-name.
    4. When there is more than one rsult, SET uses the original value of val in each operation. Subscript or index evaluation for rsult occurs immediately before its value changes.
    5. Table 6-18 shows the validity of operand combinations. An asterisk (*) means that no conversion occurs during the SET operation.

    Table 6-18 Validity of Operand Combinations in Format 1 SET Statements
    Sending Item Receiving Item
      Integer Data Item Index Index Data Item
    Integer Literal Invalid/Rule 7 Valid/Rule 5 Invalid/Rule 6
    Integer Data Item Invalid/Rule 7 Valid/Rule 5 Invalid/Rule 6
    Index Valid/Rule 7 Valid/Rule 5 Valid/Rule 6*
    Index Data Item Invalid/Rule 7 Valid/Rule 5* Valid/Rule 6*

    Format 2

    1. The SET statement increments (UP) or decrements (DOWN) indx by a value that corresponds to the number of occurrences increm represents.
    2. When there is more than one indx, SET uses the original value of increm in each operation.

    Format 3

    1. SET moves the literal in the VALUE clause for cond-name to its associated conditional variable. The transfer occurs according to the rules for elementary moves. If the VALUE clause contains more than one literal, the first is moved.

    Format 4

    1. SET changes the status of each switch-name in the statement.
    2. The ON phrase changes the status of switch-name to on.
    3. The OFF phrase changes the status of switch-name to off.
    4. The SET statement changes the switch status only for the image in which it executes. When the image terminates, the status of each external switch is the same as when the image began.

    Format 5

    1. The address of identifier is evaluated and stored in pointer-id.

    Format 6

    1. Specifying the SUCCESS option sets status-code-id to the SUCCESS state (the low-bit of status-code-id is set to 1).
    2. Specifying the FAILURE option sets status-code-id to the FAILURE state (the low-bit of status-code-id is set to 0).

    Additional References

    Examples

    The examples assume these Environment and Data Division entries:


     SPECIAL-NAMES.
        SWITCH 1 UPDATE-RUN ON STATUS IS DO-UPDATE
        SWITCH 3 REPORT-RUN ON STATUS IS DO-REPORT
            OFF STATUS IS SKIP-REPORT
        SWITCH 4 IS NEW-YEAR ON STATUS IS BEGIN-YEAR
            OFF IS CONTINUE-YEAR.
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01    YEAR-LEVEL            PIC 99.
        88    FRESHMAN VALUE 1.
        88    SOPHOMORE VALUE 2.
        88    JUNIOR VALUE 3.
        88    SENIOR VALUE 4.
        88    FIRST-MASTERS VALUE 5.
        88    MASTERS VALUE 5,6.
        88    FIRST-DOCTORAL VALUE 7.
        88    DOCTORAL VALUE 7,8.
        88    NON-DEGREE-UNDERGRAD VALUE 9.
        88    NON-DEGREE-GRAD VALUE 10.
        88    UNDERGRAD VALUE 9, 1 THROUGH 4.
        88    GRAD VALUE 10, 5 THROUGH 8.
    01    COURSES-AVAILABLE.
        02    OCCURS 100 TIMES INDEXED BY COURSE-INDEX.
            03    COURSE-NAME             PIC X(10).
            03    COURSE-INSTRUCTOR       PIC X(20).
            03    COURSE-LOCATION         PIC X(10).
            03    COURSE-CODE             PIC 9(5).
    01    POINTER-VAL USAGE IS POINTER.
    01    THREE-DIMENSIONAL-TABLE.
        02    X OCCURS 5 TIMES INDEXED BY I.
            03    Y OCCURS 7 TIMES INDEXED BY J.
                04    Z     PIC X(17) OCCURS 3 TIMES.
    01    K                 PIC S9(9) COMP.
    01    RETURN-STATUS     PIC S9(9) COMP.
    01    DECREMENT-VALUE   PIC 9 VALUE 1.
    
    1. Format 1---Initializing COURSE-INDEX.


      SET COURSE-INDEX TO 5.
      
    2. Format 2---Adding to or subtracting from the index-name COURSE-INDEX.


      SET COURSE-INDEX UP BY 1.
      
      SET COURSE-INDEX DOWN BY DECREMENT-VALUE.
      
    3. Format 3---Initializing a conditional variable:
      YEAR-LEVEL


      Previous Next Contents Index