Monday, 29 July 2013

Easytrieve Sorting.

SORT

SORT is an activity that sequences an input file on fields specified as keys. SORT uses the interface to your system sort. You can sort on as many fields as your system allows.
SORT file-name-1     TO       file-name-2               +
           USING    (field-name [D] …)                       +
           NAME sort-name
         
          [ BEFORE proc-name ]

File-name-1 is the input file to be sorted.

File-name-2 is the output file.

USING field-name identifies those fields from file-name-1 that you use as sort keys. Specify sort keys in major to minor order.

D optionally sorts the field contents in descending order (default = ascending order).

NAME sort-name identifies the sort activity for documentation purposes.

Example

FILE PERSNL FB ( 150 1800 )
           NAME              1      10       A
           DEPT              11       5       N
           GROSS-PAY 16       4       P    2
FILE PAY-SORT FB ( 150 1800 )
      SORT PERSNL TO PAY-SORT               +
                USING ( DEPT GROSS-PAY )      +
                NAME SORT-EXAMPLE-1

SORT Procedure

You use SORT procedure to select only certain records for sorting and / or to modify the contents of records before a sort.
  • A SORT procedure immediately follows the SORT statement.
  • You invoke a SORT procedure with the BEFORE parameter.
  • The SORT procedure executes for each record from file-name-1 prior to passing the record to the sort.
Example
This example illustrates the use of the SORT activity and SORT procedures.

FILE PERSNL FB (150 1800)
          NAME 1 10 A
          DEPT 11 5 N
          GROSS-PAY 16 4 P 2
FILE PAYSORT F(19) VIRTUAL
          SORT-NAME 1 10 A
          SORT-DEPT 11 5 N
          SORT-GROSS-PAY 16 4 P 2
JOB INPUT PERSNL NAME ACT-1
          PRINT RPT1
REPORT
       LINE 1 NAME DEPT GROSS-PAY
SORT PERSNL TO PAYSORT USING (SORT-DEPT + SORT-GROSS-PAY D) BEFORE SELECT-SORT NAME SORT-ACT
SELECT-SORT. PROC
         IF SORT-GROSS-PAY GE 500
                   SELECT
         END-IF
END-PROC
JOB INPUT PAYSORT NAME SORT-PROC-EX
** Logic **


GET Statement

The GET statement retrieves the next record of the named file into the file input area.
GET file-name

file-name - identifies the input file

(EOF) - Test for end-of-file (EOF) when using the GET command.

Example FILE MASTER FB ( 150 1800 )
         EMP#         9       5        N
         NAME      17     16        A
         GROSS   94       4        P      2
JOB INPUT NULL NAME READ-SEQ-MAN
GET MASTER
           IF EOF MASTER
                  STOP
           END-IF
          IF GROSS > 500
               PRINT RPT1
          END-IF
REPORT RPT1
        LINE 1 EMP# NAME GROSS

* You use NULL in the JOB statement to inhibit automatic input.

PUT Statement

The PUT statement outputs a file sequentially.
Example 1

FILE PERSNL FB ( 150 1800 )
       EMP#          9      5     N
       NAME       17     16    A
       GROSS     94      4     P   2
FILE NEWPAY2 F(20) VIRTUAL RETAIN
       NAME          1     16    A
       GROSS     17       4     P  2
JOB INPUT PERSNL NAME PUT-EXAMPLE
       ** Logic **
MOVE LIKE PERSNL TO NEWPAY2
PUT NEWPAY2


No comments:

Post a comment