HP 3000 Manuals

Procedure Division for File Input and Output (cont) [ COBOL/HP-UX Language Reference for the Series 700 and 800 ] MPE/iX 5.0 Documentation


COBOL/HP-UX Language Reference for the Series 700 and 800

Procedure Division for File Input and Output (cont) 

The RELEASE Statement 

Function 

The RELEASE statement transfers records to the initial phase of a SORT
operation.

General Format 

     RELEASE record-name [ FROM identifier]

Syntax Rules 

   1.  A RELEASE statement can only be used within the range of an input
       procedure associated with a SORT statement for a file whose
       sort-merge file description entry contains record-name.  (See the
       section "The SORT Statement"  in this chapter.)

   2.  Record-name must be the name of a logical record in the associated
       sort-merge file description entry and can be qualified.

   3.  If identifier is a function-identifier, it must reference an 
       alpha-numeric function. When identifier is not a 
       function-identifier, (ANS85) record-name and identifier must not
       reference the same storage area.
________________________________________________________________________
|(OSVS) (VSC2) (MF)                                                    |
|                                                                      |
|                                                                      |
|                                                                      |
|4.   Record-name can be defined as a floating-point item.             |
|                                                                      |
|5.   Identifier can be a floating-point item.                         |
|                                                                      |
________________________________________________________________________

            

General Rules 

   1.  The execution of a RELEASE statement causes the record named by
       record-name to be released to the initial phase of a sort
       operation.

   2.  If the FROM phrase is used, the contents of the identifier data
       area are moved to record-name, then the contents of record-name
       are released to the sort file.  Moving records takes place
       according to the rules specified for the MOVE statement without
       the CORRESPONDING phrase.  The information in the record area is
       no longer available, but the information in the data area
       associated with identifier is available.

   3.  After the execution of the RELEASE statement, the logical record
       is no longer available in the record area unless the associated
       sort-merge file is named in a SAME RECORD AREA clause.  The
       logical record is also available to the program as a record of
       other files referenced in the same SAME RECORD AREA clause as the
       associated sort-merge file, as well as to the file associated with
       record-name.  When control passes from the input procedure, the
       file consists of all those records which were placed in it by the
       execution of RELEASE statements.

The RETURN Statement 

Function 

The RETURN statement obtains either sorted records from the final phase
of a SORT operation or merged records during a MERGE operation.

General Format 

[]
Syntax Rules 1. File-name must be described by a sort-merge file description entry in the Data Division. 2. A RETURN statement can only be used within the range of an output procedure associated with a SORT or MERGE statement for file-name. 3. The INTO phrase must not be used when the input file contains logical records of various sizes as indicated by their record descriptions. The storage area associated with identifier and the record area associated with file-name must not be the same storage area. ________________________________________________________________________ |(OSVS) (VSC2) (MF) | | | | | | | |4. Identifier-1 can be a floating-point item. | | | ________________________________________________________________________ General Rules 1. When the logical records of a file are described with more than one record description, these records automatically share the same storage area; this is equivalent to an implicit redefinition of the area. The contents of any data items which lie beyond the range of the current data record are undefined at the completion of the execution of the RETURN statement. 2. The execution of the RETURN statement causes the next existing record in the file referenced by file-name, as determined by the keys listed in the SORT or MERGE statement, to be made available in the record area associated with file-name. If no next logical record exists in the file referenced by file-name, the at end condition exists and control is transferred to imperative-statement-1 of the AT END phrase. Execution continues according to the rules for each statement specified in imperative-statement-1. If a procedure branching or conditional statement which causes explicit transfer of control is executed, control is transferred according to the rules for that statement. Otherwise, upon completion of the execution of imperative-statement-1, control is transferred to the end of the RETURN statement ________________________________________________________________________ |(ANS85) | |and the NOT AT END phrase if specified is ignored. | ________________________________________________________________________ When the at end condition occurs, execution of the RETURN statement is unsuccessful and the contents of the record area associated with file-name are undefined. After the execution of imperative-statement-1 in the AT END phrase, no RETURN statement can be executed as part of the current output procedure. ________________________________________________________________________ |(ANS85) | | | | | | | |3. If an at end condition does not occur during the execution of a | | RETURN statement, then after the record is made available and | | after executing any implicit move resulting from the presence of | | an INTO phrase, control is transferred to imperative-statement-2,| | if specified. Otherwise, control is transferred to the end of | | the RETURN statement. | | | |4. The END-RETURN phrase delimits the scope of the RETURN | | statement. | | | ________________________________________________________________________ 5. The INTO phrase can be specified in a RETURN statement: a. If only one record description is subordinate to the sort-merge file description entry, or: b. If all record-names associated with file-name and the data item referenced by identifier describe a group item or an elementary alphanumeric-item. 6. The result of the execution of a RETURN statement with the INTO phrase is equivalent to the application of the following rules in the order specified: a. The execution of the same RETURN statement without the INTO phrase. b. The current record is moved from the record area to the area specified by identifier according to the rules for the MOVE statement without the CORRESPONDING phrase. The size of the current record is determined by rules specified for the RECORD clause. If the file description entry contains a RECORD IS VARYING clause, the implied move is a group move. The implied MOVE statement was unsuccessful. Any subscripting associated with identifier-1 is evaluated after the record has been read and immediately before it is moved to the data item. The record is available in both the record area and the data item referenced by identifier-1. The REWRITE Statement Function The REWRITE statement logically replaces a record existing in a disk file. General Formats
[]
Format 2 (Record Sequential Files)
[]
Format 3 (Relative and Indexed Files)
[]
Syntax Rules All Formats (All Files) 1. If identifier is a function-identifier, it must reference an alpha-numeric function. When identifier is not a function-identifier, (ANS85) record-name and identifier must not reference the same storage area. 2. Record-name is the name of a logical record in the File Section of the Data Division and can be qualified. ________________________________________________________________________ |(OSVS) (VSC2) (MF) | | | | | | | |3. Record-name can define a floating point or DBCS item. | | | |4. Identifier can be a USAGE DISPLAY-1 (DBCS) item or a floating- | | point item. | | | ________________________________________________________________________ Format 3 (Relative And Indexed Files) 5. The INVALID KEY phrase must not be specified for a REWRITE statement which references a file in sequential access mode. ________________________________________________________________________ |(OSVS) (VSC2) | |However, it can be specified for an indexed file in sequential access | |mode. | ________________________________________________________________________ 6. The INVALID KEY phrase must be specified in the REWRITE statement for files in the random or dynamic access mode for which an appropriate USE procedure is not specified. General Rules All Formats (All Files) 1. The file associated with record-name must be a disk file and must be open in the I/O mode at the time of execution of this statement. (See the section "The OPEN Statement" earlier in this chapter.) 2. For files in the sequential access mode the last input-output statement executed for the associated file prior to the execution of the REWRITE statement must have been a successfully executed READ statement. The operating system logically replaces the record that was accessed by the READ statement. 3. The execution of a REWRITE statement with the FROM phrase is equivalent to the execution of: MOVE identifier TO record-name followed by the execution of the same REWRITE statement without the FROM phrase. The contents of the record area prior to the execution of the implicit MOVE statement have no effect on the execution of the REWRITE statement. 4. The file position indicator is not affected by the execution of a REWRITE statement. 5. The execution of the REWRITE statement causes the value of the FILE STATUS data item, if any, associated with the file to be updated. (See the section "I/O Status" earlier in this chapter.) ________________________________________________________________________ |(ANS85) | | | | | | | |6. The END-REWRITE phrase delimits the scope of the REWRITE | | statement. | | | |7. A REWRITE will not be successful if any other run unit holds a | | lock on the record to be rewritten. | | | ________________________________________________________________________ Formats 1 and 2 (Sequential Files) 8. The number of character positions in the record referenced by record-name must be equal to the number of character positions in the record being replaced. 9. The logical record released by a successful execution of the REWRITE statement is no longer available in the record area unless the associated file is named in a SAME RECORD AREA clause. In this case, the record is still available to the program in the record area, not only as a record of this file, but also as a record of other files named in the SAME RECORD AREA clause. ________________________________________________________________________ |(MF) | | | |Format 1 (Line Sequential Files) | | | | | | | |10. The REWRITE statement can be used and is considered successful | | whenever the record being written (after compacting by removing | | trailing spaces, tab compression and null insertion) is | | less than or equal to the length of the original compacted | | record. When a record is read, nulls and tab characters | | in the record can be expanded and this makes the record | | longer. | | | ________________________________________________________________________ Format 3 (Relative And Indexed Files) 11. The logical record released by a successful execution of the REWRITE statement is no longer available in the record area unless the associated file is saved in a SAME RECORD AREA clause, in which case the logical record is available both to the program as a record of other files appearing in the same SAME RECORD AREA clause as the associated I/O file, as well as to the file associated with record-name. ________________________________________________________________________ |(MF) | | | | | | | |12. If a file has multiple record locking, the REWRITE statement can | | be made to acquire a record lock by use of the WRITELOCK | | directive. See your COBOL System Reference for details of this | | directive. | | | ________________________________________________________________________ Format 3 (Relative Files) 13. For a file accessed in either random or dynamic access mode, the operating system logically replaces the record specified by the contents of the RELATIVE KEY data item associated with the file. If the file does not contain the record specified by the key, the INVALID KEY condition exists. (See the section The INVALID KEY Condition in this chapter.) The updating operation does not take place and the data in the record area is unaffected. Format 3 (Indexed Files) 14. For a file in sequential access mode, the record to be replaced is specified by the value contained in the prime record key. When the REWRITE statement is executed, the value contained in the prime record data item of the record to be replaced must be equal to the value of the prime record key of the last record read from this file. 15. For a file in random or dynamic access mode, the record to be replaced is specified by the prime record key data item. 16. The contents of alternate record key data items of the record being rewritten can differ from those in the record being replaced. The operating system uses the contents of the record key data items during execution of the REWRITE statement so that the record can subsequently be accessed via any of those specified record keys. 17. The INVALID KEY condition exists when: a. Access mode is sequential and the value contained in the prime record key data item of the record to be replaced is not equal to the value of the prime record key of the last record read from this file, or: b. The value contained in the prime record key data item does not equal that of any record stored in the file, or: c. The value contained in an alternate record key data item for which a DUPLICATES clause has not been specified is equal to that of a record already stored in the file. The updating operation will not take place and the data in the record area will be unaffected. (See the section "The INVALID KEY Condition" in this chapter.) ________________________________________________________________________ |The ROLLBACK Statement | | | |(MF) | | | |Function | | | |The ROLLBACK statement releases all record locks in all files held by | |this run unit. For COBOL systems that support the WITH...ROLLBACK | |clause of the SELECT statement as other than documentary, the ROLLBACK| |statement indicates the end of the current transaction and the effects| |of that transaction are cancelled. | | | |General Format | | | | ROLLBACK | | | |General Rules | | | | | | 1. Execution of the ROLLBACK statement causes all record locks in | | all files held by the run-unit to be released. | | | | 2. The file lock on an exclusive file is not affected by the | | ROLLBACK statement. | | | | 3. If your COBOL system supports the WITH ROLLBACK clause of the | | SELECT statement as other than documentary, then the ROLLBACK | | verb: | | | | | | a. Indicates completion of the current transaction | | | | b. Ensures all changes applied during the transaction are | | cancelled. | ________________________________________________________________________ The SORT Statement Function The SORT statement creates a sort file by executing input procedures or by transferring records from another file, sorts the records in the sort file on a set of specified keys, and in the final phase of the sort operation, makes available each record from the sort file, in sorted order to some output procedures or to an output file. ________________________________________________________________________ |(MF) | |The SORT statement can also be used to sort the elements of a | |table. | ________________________________________________________________________ General Formats Format 1
[]
[]
Syntax Rules All Formats 1. SORT statements can appear anywhere except in the declaratives portion of the Procedure Division or in an input or output procedure associated with a SORT or MERGE statement. ________________________________________________________________________ |(MF) | |A format 2 SORT can appear in the Declaratives Section. | ________________________________________________________________________ 2. Data-name-1 is a KEY data-name and is subject to the following rules: a. KEY data-names can be qualified. b. The data items identified by KEY data-names must not be variable length items. c. KEY data items can be floating-point items. ________________________________________________________________________ |(OSVS) (VSC2) (MF) | | | | | | | |d. If the KEY is an external floating-point item, the compiler will | | treat the data item as character data, rather than numeric data. | | The sequence in which the records are sorted depends on the | | collating sequence used. | | | |e. If the KEY data item is internal floating-point, the sequence of | | key values will be in numeric order. | | | ________________________________________________________________________ Format 1 3. File-name-1 must be described in a Sort-Merge File Description entry in the Data Division. 4. Data-name-1 is a KEY data-name and is subject to the following rules: a. The data items identified by KEY data-names must be described in records associated with file-name-1. b. If file-name-1 has more than one record description, then the data items identified by KEY data-names need be described in only one of the record descriptions. c. None of the data items identified by KEY data-names can be described by an entry which either contains an OCCURS clause or is subordinate to an entry which contains an OCCURS clause. d. If the file referenced by file-name-1 contains variable length records, all the data items identified by key data-names must be contained within the first xcharacter positions of the record, where x equals the minimum record size specified for the file referenced by file-name-1. 5. The words THRU and THROUGH are equivalent. 6. File-name-2 and file-name-3 must be described in the file description entry, not in a sort-merge file description entry,in the Data Division. ________________________________________________________________________ |(ANS85) | | | | | | | |7. The files referenced by file-name-2 and file-name-3 can reside on| | the same multiple file reel. | | | |8. If file-name-3 references an indexed file, the first | | specification of data-name-1 must be associated with an ASCENDING| | phrase and the data item referenced by that data-name-1 must | | occupy the same character positions in its record as the | | data item associated with the prime record key for that | | file. | | | ________________________________________________________________________ 9. No pair of file-names in the same SORT statement can be specified in the SAME SORT AREA or SAME SORT-MERGE AREA clause. File-names associated with the GIVING phrase can not be specified in the same SAME clause. ________________________________________________________________________ |(ANS85) | | | | | | | |10. If the GIVING phrase is specified and the file referenced by | | file-name-3 contains variable length records, the size of the | | records contained in the file referenced by file-name-1 must not | | be less than the smallest record nor larger than the largest | | record described for file-name-3. If the file referenced by | | file-name-3 contains fixed length records, the size of the | | records contained in the file referenced by file-name-1 must not | | be larger than the largest record described for the file | | referenced by file-name-3. | | | | If the data descriptions of the elementary items that make up | | these records are not identical, it is the programmer's | | responsibility to describe the corresponding records in such a | | manner as to cause equal amounts of character positions to be | | allocated for the corresponding records. | | | ________________________________________________________________________ ________________________________________________________________________ |(ANS85) | | | | | | | |11. If the USING phrase is specified and the file referenced by | | file-name-1 contains variable length records, the size of the | | records contained in the file referenced by file-name-2 must not | | be less than the smallest record nor larger than the largest | | record described for file-name-1. If the file referenced by | | file-name-1 contains fixed length records, the size of the | | records contained in the file referenced by file-name-2 must not | | be larger than the largest record described for the file | | referenced by file-name-1. | | | ________________________________________________________________________ 12. Procedure-name-1 represents the name of an input procedure. Procedure-name-3 represents the name of an output procedure. 13. Procedure-name-1, procedure-name-2, procedure-name-3 and procedure-name-4 must be section-names. ________________________________________________________________________ |This restriction is removed. (ANS85) (OSVS) | ________________________________________________________________________ ________________________________________________________________________ |(MF) | | | |Format 2 | | | | | | | |14. Data-name-2 can be qualified and must have an OCCURS clause in | | the data description entry. | | | |15. The data item referenced by data-name-1 must be the same as the | | data item referenced by data-name-2, or an entry subordinate to | | the data item referenced by data-name-2. | | | |16. Unless data-name-1 and data-name-2 are the same, the data item | | referenced by data-name-1 must not be described by an entry | | containing an OCCURS clause. The data item referenced by | | data-name-1 must not be subordinate to an entry containing an | | OCCURS clause that is also subordinate to data-name-2. | | | |17. The KEY phrase can be omitted only if the description of the | | table referenced by data-name-2 contains a KEY phrase. | | | |18. If data-name-2 references a data item subordinate to a data | | item that contains an OCCURS clause, an index-name must | | be associated with each data item containing an OCCURS | | clause to which the data item referenced by data-name-2 is | | subordinate. | | | ________________________________________________________________________ General Rules All Formats 1. The words ASCENDING and DESCENDING are transitive across all occurrences of data-name-1 until another word ASCENDING or DESCENDING is encountered. 2. The data items referenced by the specifications of data-name-1 are the key data items which determine the order in which records are returned from the file referenced by file-name-1 or the order in which the table elements are stored after sorting takes place. The order of significance of the keys is the order in which they are specified in the SORT statement, without regard to their association with ASCENDING or DESCENDING phrases. 3. If the DUPLICATES phrase is specified and the contents of all the key data items associated with one data record or table element are equal to the contents of the corresponding key data items associated with one or more other data records or table elements, the order of return of these records is: a. The order of the associated input files as specified in the SORT statement, or by means of a run-time switch. See your COBOL System Reference for details of run-time switches. Within a given input file the order is that in which the records are accessed from that file. b. The order in which these records are released by an input procedure, when an input procedure is specified. c. The relative order of the contents of these table elements before sorting takes place. 4. If the DUPLICATES phrase is not specified and the contents of all the key data items associated with one data record or table element are equal to the contents of the corresponding key data items associated with one or more other data records or table elements, the order of return of these records or the relative order of the contents of these table elements is undefined. 5. The collating sequence that applies to the comparison of the nonnumeric key data items specified is determined at the beginning of the execution of the SORT statement in the following order of precedence: a. First, the collating sequence established by the COLLATING SEQUENCE phrase, if specified, in the SORT statement. b. Second, the collating sequence established as the program collating sequence. Format 1 6. The SORT statement releases all the records in the file referenced by file-name-2 or released by an input procedure to the file referenced by file-name-1, and returns them to an output procedure, or to the file referenced by file-name-3, in an order determined by the ASCENDING and DESCENDING phrases and the values of the data items referenced by the specifications of data-name-1. 7. To determine the relative order in which two records returned from the file referenced by file-name-1, the contents of corresponding key data items are compared according to the rules for comparison of operands in a relation condition, starting with the most significant key data item: a. If the contents of the corresponding key data items are not equal and the key is associated with the ASCENDING phrase, the record containing the key data item with the lower value is returned first. b. If the contents of the corresponding key data items are not equal and the key is associated with the DESCENDING phrase, the record containing the key data item with the higher value is returned first. c. If the contents of the corresponding key data items are equal, the determination is made on the contents of the next most significant key data item. 8. The execution of the SORT statement consists of three distinct phases as follows: a. Records are made available to the file referenced by file-name-1. This is achieved either by the execution of RELEASE statements in the input procedure or by the implicit execution of READ statements for file-name-2. When this phase commences, the file referenced by file-name-2 must not be in the open mode. When this phase terminates, the file referenced by file-name-2 is not in the open mode. b. The file referenced by file-name-1 is sequenced. No processing of the files referenced by file-name-2 and file-name-3 takes place during this phase. c. The records of the file referenced by file-name-1 are made available in sorted order. The sorted records are either written to the file referenced by file-name-3, or, by the execution of a RETURN statement, are made available for processing by the output procedure. When this phase commences, the file referenced by file-name-3 must not be in the open mode. When this phase terminates, the file referenced by file-name-3 is not in the open mode. 9. The input procedure can consist of any procedure needed to select, modify or copy the records that are made available one at a time by the RELEASE statement to the file referenced by file-name-1. The range includes all statements that are executed as a result of a transfer of control by CALL, EXIT without the optional PROGRAM phrase, GO TO and PERFORM statements in the range of the input procedure. The range of the input procedure must not cause the execution of any MERGE, RETURN or SORT statement. 10. If an input procedure is specified, control is passed to the input procedure before the file referenced by file-name-1 is sequenced by the SORT statement. The compiler inserts a return mechanism at the end of the last statement in the input procedure and when control passes the last statement in the input procedure, the records that have been released to the file referenced by file-name-1 are sorted. 11. If the USING phrase is specified, all the records in the file(s) referenced by file-name-2 are transferred to the file referenced by file-name-1. For each of the files referenced by file-name-2 the execution of the SORT statement causes the following actions to be taken: a. The processing of the file is initiated. The initiation is performed as if an OPEN statement with the INPUT phrase had been executed. b. The logical records are obtained and released to the sort operation. Each record is obtained as if a READ statement with the NEXT and AT END phrases had been executed. If the file referenced by file-name-1 contains fixed length records, any record in the file referenced by file-name-2 containing fewer character positions than that specified for file-name-1 is space-filled on the right, beginning with the first character position after the last character in the record, when that record is released to the file referenced by file-name-1. If the file referenced by file-name-1 is described with variable-length records, the size of any record written to file-name-1 is the size of that record when it was read from file-name-2 or file-name-3, regardless of the contents of the data item referenced by a RECORD VARYING DEPENDING ON or OCCURS DEPENDING ON clause specified in the File Description entry for file-name-1. c. The processing of the file is terminated. The termination is performed as if a CLOSE statement without optional phrases had been executed. This termination is performed before the file referenced by file-name-1 is sequenced by the SORT statement. For a relative file, the content of the Relative Key data item is undefined after the execution of a SORT statement if file-name-2 is not referenced in the GIVING phrase. These implicit functions are performed such that any associated USE AFTER EXCEPTION procedures are executed; however, the execution of such a USE procedure must not cause the execution of any statement manipulating the file referenced by file-name-2 or accessing the record area associated with file-name-2. The value of the data item referenced by a RECORD VARYING DEPENDING ON clause specified in the File Description entry for file-name-2 is undefined upon completion of the SORT statement. 12. The output procedure can consist of any procedure needed to select, modify or copy the records that are made available one at a time by the RETURN statement in sorted order from the file referenced by file-name-1. The range includes all statements that are executed as a result of a transfer of control by CALL, EXIT without the optional PROGRAM phrase, GO TO and PERFORM statements in the range of the output procedure, as well as all statements in the Declarative procedures that are executed as a result of the execution of statements in the range of the output procedure. The range of the input procedure must not cause the execution of any MERGE, RELEASE or SORT statement. 13. If an output procedure is specified, control passes to it after the file referenced by file-name-1 has been sequenced by the SORT statement. The compiler inserts a return mechanism at the end of the last statement in the output procedure and when control passes the last statement in the output procedure, the return mechanism provides for termination of the sort and then passes control to the next executable statement after the SORT statement. Before entering the output procedure, the sort procedure reaches a point at which it can select the next record in sorted order when requested. The RETURN statements in the output procedure are the requests for the next record. 14. If the GIVING phrase is specified, all the sorted records are written on the file referenced by file-name-3 as the implied output procedure for the SORT statement. For each of the files referenced by file-name-3, the execution of the SORT statement causes the following actions to be taken: a. The processing of the file is initiated. The initiation is performed as if an OPEN statement with the OUTPUT phrase had been executed. This initiation is performed after the execution of any input procedure. b. The logical records are returned and written onto the file. The records are written as if a WRITE statement without any optional phrases had been executed. If the file referenced by file-name-3 is described with fixed-length records, any record in the file referenced by file-name-1 containing fewer character positions than that specified for file-name-3 is space-filled on the right, beginning with the first character position after the last character in the record, when that record is returned to the file referenced by file-name-3. If the file referenced by file-name-3 is described with variable-length records,the size of any record written to file-name-3 is the size of that record when it was read from file-name-1, regardless of the contents of the data item referencedby a RECORD VARYING DEPENDING ON or OCCURS DEPENDING ON clause specified in the File Description entry for file-name-3. For a relative file, the Relative Key data item for the first record returned contains the value "1"; for the second record returned, the value "2", and so on. The content of the Relative Key data item is undefined after execution of a SORT statement. c. The processing of the file is terminated. The termination is performed as if a CLOSE statement without optional phrases had been executed. These implicit functions are performed such that any associated USE AFTER EXCEPTION procedures areexecuted; however, the execution of such a USE procedure must not cause the execution of any statement manipulating the file referenced by file-name-3 or accessing the record area associatedwith file-name-3. On the first attempt to write beyond the externally-defined boundaries of a file, the USE AFTER EXCEPTION procedure, if any, associated with the file is executed; if control is returned as specified by the rules of the USE statement, no additional implicit write operations are executed for that file and the processing of the file is terminated as specified in part c. The value of the data item referenced by a RECORDING VARYING DEPENDING ON clause specified in the File Description entry for file-name-1 is undefined upon completion of the SORT statement for which the GIVING phrase is specified. 15. Segmentation, as defined in the chapter Segmentation, can be applied to programs containing the SORT statement. However, the following restrictions apply: a. If a SORT statement appears in a section that is not in an independent segment, then any input procedures or output procedures referenced by that SORT statement must appear: * Totally within non-independent segments, or * Wholly contained in a single independent segment. b. If a SORT statement appears in an independent segment, then any input procedures or output procedures referenced by that SORT statement must be contained: * Totally within non-independent segments, or * Wholly within the same independent segment as that SORT statement. ________________________________________________________________________ |(OSVS) (VSC2) (MF) | | | | | | | |16. The SORT-RETURN special register is available to SORT programs. | | It contains a return code of of 0 (successful) or 16 | | (unsuccessful) at the completion of a sort operation. You can | | set the SORT-RETURN special register to 16 in an error | | declarative or input/output procedure to terminate a sort | | operation before all records are processed. The operation is | | terminated on the next RETURN or RELEASE statement. | | | ________________________________________________________________________ ________________________________________________________________________ |(MF) | | | |Format 2 | | | | | | | |17. The SORT statement sorts the table referenced by data-name-2 and | | presents the sorted table in data-name-2 either in the order | | determined by the ASCENDING or DESCENDING phrases, if specified, | | or in the order determined by the KEY phrase associated with | | data-name-2. | | | |18. To determine the relative order in which the table elements are | | stored after sorting, the contents of the corresponding key data | | items are compared according to the rules for comparison of | | operands in a relation condition, starting with the most | | significant key data item. | | | | | | a. If the contents of the corresponding key data items are | | not equal and the key is associated with the ASCENDING | | phrase, the table element containing the key data | | item with the lower value has the lowest occurrence | | number. | | | | b. If the contents of the corresponding key data items are | | not equal and the key is associated with the DESCENDING | | phrase, the table element containing the key data item | | with the higher value has the lowest occurrence | | number. | | | | c. If the contents of the corresponding key data items are | | equal, the determination is based on the contents of the | | next most significant key data item. | | | |19. The number of occurrences of table elements referenced | | by data-name-2 is determined by the rules in the OCCURS | | clause. | | | |20. If data-name-2 references a data item which is subordinate to a | | data item which contains an OCCURS clause, the first or only | | index-name associated with each such superordinate table must be | | set to the desired occurrence number before the SORT statement is| | executed. | | | |21. If the KEY phrase is not specified, the sequence is determined by| | the KEY phrase in the Data Description entry of the table | | referenced by data-name-2. | | | |22. If the KEY phrase is specified, it overrides any KEY phrase | | specified in the Data Description entry of the table referenced | | by data-name-2. | | | |23. If data-name-1 is omitted, the data item referenced by | | data-name-2 is the key data item. | | | ________________________________________________________________________ ________________________________________________________________________ |(MF) | | | | | | | |24. The sorted table elements of the table referenced by data-name-2 | | are placed in the table referenced by data-name-2. | | | | Examples of Table Sorts | | | | A table sort using KEYS in OCCURS clause for sequencing: | | | | working-storage section. | | 01 group-item. | | 05 tabl occurs 10 times | | ascending elem-item2 | | descending elem-item1. | | 10 elem-item1 pic x. | | 10 elem-item2 pic x. | | . . . | | procedure division. | | . . . | | sort tabl. | | if tabl (1) . . . | | | | This is a simple sort in which the table is sorted in ascending | | order using the key definitions in the OCCURS clause of data item| | Tabl to determine the sequence, that is Elem-Item2 would be the | | major key (ascending) and Elem-Item1 would be the secondary key | | (descending). | | | | A table sort using the entire element for sequencing: | | | | working-storage section. | | 01 group-item. | | 05 tabl occurs 10 times | | 10 elem-item1 pic x. | | 10 elem-item2 pic x. | | . . . | | procedure division. | | . . . | | sort tabl ascending. | | if tabl (1) ... | | | | This is a simple sort in which the table is sorted in ascending | | order using each entire element of the table to determine the | | sequence. | | | ________________________________________________________________________ ________________________________________________________________________ |(MF) | | | |A table sort with specified items for sequencing: | | | | working-storage section. | | 01 group-item. | | 05 tabl occurs 10 times | | ascending elem-item3 | | descending elem-item1. | | 10 elem-item1 pic x. | | 10 elem-item2 pic x. | | 10 elem-item3 pic x. | | . . . | | procedure division. | | . . . | | sort tabl descending elem-item2 elem-item3 | | if tabl (1) ... | | | |This is a sort in which the table is sorted based on specified key | |data items. The major key would be Elem-Item2, even though it was not| |specified as a KEY in the OCCURS clause. The secondary key would be | |Elem-Item3. It would be treated as a DESCENDING key for this sort | |because the DESCENDING (which is transitive across KEY data items) | |specified in the SORT statement would take precedence over the | |ASCENDING specified in the OCCURS clause. | | | |A table sort for a nested table: | | | | working-storage section. | | 01 group-item. | | 05 tabl1 occurs 10 times | | indexed by t1-ind t2-ind. | | 10 tabl2 occur 5 times. | | 15 group1. | | 20 elem-item1 pic x. | | 15 group2. | | 20 elem-item1 pic 9. | | . . . | | procedure division. | | . . . | | set t1-ind to 3 | | sort tabl2 descending elem-item1 of group2 | | if group1 (3 1) ... | | | |This sorts only the third instance of Tabl2, that is Tabl2(3). It | |uses the qualified data-item, Elem-Item1 of Group2 as its key. In | |normal Procedure Division references, Elem-Item1 of Group2 would | |require two levels of subscripting/indexing while in this reference it| |has none. (Similarly, Tabl2 normally requires one level of | |subscripting, but cannot be subscripted as data-name-2 in the SORT | |statement. Instead it uses the value of T1-Ind for determining which | |instance is to be sorted.) | ________________________________________________________________________


MPE/iX 5.0 Documentation