Sie sind auf Seite 1von 4

F* F* SORT EXAMPLE F* John McKay Aug 2008 F* F* This example illustrates how to use a physical file F* to sort the

records in a subfile. F* F* The user places the cursor on a particular data field and F* presses (in this example) F8 or F9 to sort A or D F* respectively. F* F* RTNCSRLOC must be specified on the subfile header to identify F* the sort field / column. F* F* The physical is cleared in each call. It is then opened to F* load the records, closed, and opened to read the F* records. At end, it is closed. F* F* The records are read from the subfile and written to F* the U1RECORD field of the physical file. The field(s) F* which are to be sorted is(are) written to the index F* portion. The physical file is close and opened. The F* records are read from the physical file and written back to F* the subfile. A data structure is used to store the subfile F* fields on the physical record. F* F* The layout of the physical file is F* R UTY01PR F* U1KEY 32 F* U1RECORD 256 F* K U1KEY F* F* Only the relevant RPG is shown. This example consists of F* - File Definition for the sort file F* - Function perform sort (data definition, call, source) F* - Data definitions for relevant variables F* F* CMD ........ container for QCMDEXC F* CMDLEN ..... length of command string for QCMDEXC F* FSORT ...... sort function F* FSORTAD .... sort sequence - A = ASCENDING, D = DESCENDING F* IN08 ....... F08 from screen to request SORT A F* IN09 ....... F09 from screen to request SORT D F* IN_FAIL .... indicator to return failure status F* IN_SUCCESS . indicator to return success status F* SUBF_REC ... subfile detail record F* S1CHAR ..... character field displayed on subfile record F* S1CYMD ..... date field hidden on subfile record F* S1DATE ..... date field displayed on subfile record F* S1FIELD .... name of field to be sorted on, from RTNCSRLOC F* S1RRN ...... rrn for record in subfile F* S1RRNMAX ... maximum records in the subfile F* UTY01P ..... sort file F* UTY01PR .... record format of sort file F* U1CHAR ..... character field stored in physical file record F* U1CYMD ..... date field stored in physical file record F* U1DATADS ... data structure containing subfile record fields F* U1DATE ..... date field stored in physical file record F* U1NUMERIC .. numeric field stored in physical file record F* ZCMD ....... array with command string for QCMDEXC

F* ZCONVA4 .... conversion field F* ZCONVA4 .... conversion field F* ZCONVA7 .... conversion field F* ZCONVN7 .... conversion field F* F* D* FUTY01P UF A E K DISK D* D FSORT PR D FSORTAD 1 D* D DS D ZCONVA7 7 D ZCONVN7 7S 0 D* D DS D ZCONVA4 4 D ZCONVN4 4S 0 D* D in_fail S 1n D in_success S 1n D* D s1rrn S 4 0 D s1rrnmax S 4 0 D* D qcmdexc PR D cmd 200 D cmdlen 15P 5 D* D ZCMD S 75 COMMAND D* D U1DATADS DS D U1DATE D U1CHAR D U1NUMERIC D U1CYMD /free select; when *in08; fsort ('A'); when *in09; fsort ('D'); endsl; /end-free P* P* P* /TITLE FSORT P FSORT D FSORT D SORTAD /free

USROPN CONST

OVERLAY(ZCONVA7:1)

OVERLAY(ZCONVA4:1)

extpgm('QCMDEXC') options(*varsize) const const DIM(1) CTDATA PERRCD(1)

LIKE(S1DATE) LIKE(S1CHAR) LIKE(S1NUMERIC) LIKE(S1CYMD)

// sort ascending // sort descending

B PI 1 CONST

in_success = *off; in_fail = *off; qcmdexc (zcmd (1) : %size (zcmd )); clear *all uty01pr; clear u1data; open uty01p; for s1rrn = 1 to s1rrnmax; chain s1rrn subf_rec; u1date u1char u1numeric u1cymd select; when s2field = 'S1DATE'; zconvn7 = s1cymd; %subst(u1key:1:7) = zconva7; when s2field = 'S1CHAR'; %subst(u1key:1:4) = s1char; when s2field = 'S1NUMERIC'; zconvn4 = s1numeric; %subst(u1key:1:4) = zconva4; other; close uty01p; in_fail = *on; return; endsl; u1rrn = s1rrn; u1data = u1datads; write uty01pr; if s1rrn = 9999; leave; endif; endfor; close uty01p; open uty01p; if sortad = 'D'; setgt *hival uty01pr; endif; for s1rrn = 1 to s1rrnmax; chain s1rrn subf_rec; read uty01pr; = = = = s1date ; s1char ; s1numeric; s1cymd;

u1datads = u1data; s1date s1char s1numeric s1cymd = = = = u1date ; u1char ; u1numeric; u1cymd ;

update subf_rec; if s1rrn = 9999; leave; endif; endfor; close uty01p; in_success = *on; /end-free P FSORT ** CLRPFM UTY01P E

Das könnte Ihnen auch gefallen