|
Figure 1. EXECIO Example 1EXECIO Example 1
/***************************** REXX ********************************/
/* This exec reads from the data set allocated to INDD to find the */
/* first occurrence of the string "Jones". Upper and lowercase */
/* distinctions are ignored. */
/*******************************************************************/
done = 'no'
lineno = 0
DO WHILE done = 'no'
"EXECIO 1 DISKR indd"
IF RC = 0 THEN /* Record was read */
DO
PULL record
lineno = lineno + 1 /* Count the record */
IF INDEX(record,'JONES') \= 0 THEN
DO
SAY 'Found in record' lineno
done = 'yes'
SAY 'Record = ' record
END
ELSE NOP
END
ELSE
done = 'yes'
END
EXIT 0
Figure 2. EXECIO Example 2EXECIO Example 2
/***************************** REXX ********************************/
/* This exec copies records from data set 'my.input' to the end of */
/* data set 'my.output'. Neither data set has been allocated to a */
/* ddname. It assumes that the input data set has no null lines. */
/*******************************************************************/
"ALLOC DA('my.input') F(indd) SHR REUSE"
"ALLOC DA('my.output') F(outdd) MOD REUSE"
SAY 'Copying …'
"EXECIO * DISKR indd (FINIS"
QUEUE '' /* Insert a null line at the end to indicate end of file */
"EXECIO * DISKW outdd (FINIS"
SAY 'Copy complete.'
"FREE F(indd outdd)"
EXIT 0
Figure 3. EXECIO Example 3EXECIO Example 3
/**************************** REXX *********************************/
/* This exec reads five records from the data set allocated to */
/* MYINDD starting with the third record. It strips trailing blanks*/
/* from the records, and then writes any record that is longer than*/
/* 20 characters. The file is not closed when the exec is finished.*/
/*******************************************************************/
"EXECIO 5 DISKR myindd 3"
DO i = 1 to 5
PARSE PULL line
stripline = STRIP(line,t)
len = LENGTH(stripline)
IF len > 20 THEN
SAY 'Line' stripline 'is long.'
ELSE NOP
END
/* The file is still open for processing */
EXIT 0
Figure 4. EXECIO Example 4EXECIO Example 4
/**************************** REXX *********************************/
/* This exec reads first 100 records (or until EOF) of the data */
/* set allocated to INVNTORY. Records are placed on data stack */
/* in LIFO order. If fewer than 100 records are read, a message is */
/* issued. */
/*******************************************************************/
eofflag = 2 /* Return code to indicate end of file */
"EXECIO 100 DISKR invntory (LIFO"
return_code = RC
IF return_code = eofflag THEN
SAY 'Premature end of file.'
ELSE
SAY '100 Records read.'
EXIT return_code
Figure 5. EXECIO Example 5EXECIO Example 5
/**************************** REXX *********************************/
/* This exec illustrates the use of "EXECIO 0 ..." to open, empty, */
/* or close a file. It reads records from file indd, allocated */
/* to 'sams.input.dataset', and writes selected records to file */
/* outdd, allocated to 'sams.output.dataset'. In this example, the */
/* data set 'smas.input.dataset' contains variable-length records */
/* (RECFM = VB). */
/*******************************************************************/
"FREE FI(outdd)"
"FREE FI(indd)"
"ALLOC FI(outdd) DA('sams.output.dataset') OLD REUSE"
"ALLOC FI(indd) DA('sams.input.dataset') SHR REUSE"
eofflag = 2 /* Return code to indicate end-of-file */
return_code = 0 /* Initialize return code */
in_ctr = 0 /* Initialize # of lines read */
out_ctr = 0 /* Initialize # of lines written */
/*******************************************************************/
/* Open the indd file, but do not read any records yet. All */
/* records will be read and processed within the loop body. */
/*******************************************************************/
"EXECIO 0 DISKR indd (OPEN" /* Open indd */
/*******************************************************************/
/* Now read all lines from indd, starting at line 1, and copy */
/* selected lines to outdd. */
/*******************************************************************/
DO WHILE (return_code ¬ = eofflag) /* Loop while not end-of-file */
'EXECIO 1 DISKR indd' /* Read 1 line to the data stack */
return_code = rc /* Save execio rc */
IF return_code = 0 THEN /* Get a line ok? */
DO /* Yes */
in_ctr = in_ctr + 1 /* Increment input line ctr */
PARSE PULL line.1 /* Pull line just read from stack*/
IF LENGTH(line.1) > 10 then /* If line linger than 10 chars */
DO
"EXECIO 1 DISKW outdd (STEM line." /* Write it to outdd */
out_ctr = out_ctr + 1 /* Increment output line ctr */
END
END
END "EXECIO 0 DISKR indd (FINIS" /* Close the input file, indd */
IF out_ctr > 0 THEN /* Were any lines written to outdd?*/
DO /* Yes. So outdd is now open */
Figure 6. EXECIO Example 5 (continued)EXECIO Example 5 (continued)
/****************************************************************/
/* Since the outdd file is already open at this point, the */
/* following "EXECIO 0 DISKW ..." command will close the file, */
/* but will not empty it of the lines that have already been */
/* written. The data set allocated to outdd will contain out_ctr*/
/* lines. */
/****************************************************************/
"EXECIO 0 DISKW outdd (FINIS" /* Closes the open file, outdd */
SAY 'File outdd now contains ' out_ctr' lines.'
END
ELSE /* Else no new lines have been */
/* written to file outdd */
DO /* Erase any old records from the file*/
/****************************************************************/
/* Since the outdd file is still closed at this point, the */
/* following "EXECIO 0 DISKW …" command will open the file, */
/* write 0 records, and then close it. This will effectively */
/* empty the data set allocated to outdd. Any old records that */
/* were in this data set when this exec started will now be */
/* deleted. */
/****************************************************************/
"EXECIO 0 DISKW outdd (OPEN FINIS" /*Empty the outdd file */
SAY 'File outdd is now empty.'
END
"FREE FI(indd)"
"FREE FI(outdd)"
EXIT
Figure 7. EXECIO Example 6EXECIO Example 6
/***************************** REXX ********************************/
/* This exec uses EXECIO to successively append the records from */
/* 'sample1.data' and then from 'sample2.data' to the end of the */
/* data set 'all.sample.data'. It illustrates the effect of */
/* residual data in STEM variables. Data set 'sample1.data' */
/* contains 20 records. Data set 'sample2.data' contains 10 */
/* records. */
/*******************************************************************/
"ALLOC FI(myindd1) DA('sample1.data') SHR REUSE" /* input file 1 */
"ALLOC FI(myindd2) DA('sample2.data') SHR REUSE" /* input file 2 */
"ALLOC FI(myoutdd) DA('all.sample.data') MOD REUSE" /* output append
file */
/*******************************************************************/
/* Read all records from 'sample1.data' and append them to the */
/* end of 'all.sample.data'. */
/*******************************************************************/
exec_RC = 0 /* Initialize exec return code */
"EXECIO * DISKR myindd1 (STEM newvar. FINIS" /* Read all records */
IF rc = 0 THEN /* If read was successful */
DO
/*****************************************************************/
/* At this point, newvar.0 should be 20, indicating 20 records */
/* have been read. Stem variables newvar.1, newvar.2, ... through*/
/* newvar.20 will contain the 20 records that were read. */
/*****************************************************************/
SAY "-----------------------------------------------------"
SAY newvar.0 "records have been read from 'sample1.data': "
SAY
DO i = 1 TO newvar.0 /* Loop through all records */
SAY newvar.i /* Display the ith record */
END
"EXECIO" newvar.0 "DISKW myoutdd (STEM newvar." /* Write exactly
the number of records read */
Figure 8. EXECIO Example 6 (continued)EXECIO Example 6 (continued)
IF rc = 0 THEN /* If write was successful */
DO
SAY
SAY newvar.0 "records were written to 'all.sample.data'"
END
ELSE
DO
exec_RC = RC /* Save exec return code */
SAY
SAY "Error during 1st EXECIO … DISKW, return code is " RC
SAY
END
END
ELSE
DO
exec_RC = RC /* Save exec return code */
SAY
SAY "Error during 1st EXECIO … DISKR, return code is " RC
SAY
END
IF exec_RC = 0 THEN /* If no errors so far... continue */
DO
/***************************************************************/
/* At this time, the stem variables newvar.0 through newvar.20 */
/* will contain residual data from the previous EXECIO. We */
/* issue the "DROP newvar." instruction to clear these residual*/
/* values from the stem. */
/***************************************************************/
DROP newvar. /* Set all stem variables to their
uninitialized state */
/***************************************************************/
/* Read all records from 'sample2.data' and append them to the */
/* end of 'all.sample.data'. */
/***************************************************************/
"EXECIO * DISKR myindd2 (STEM newvar. FINIS" /*Read all records*/
IF rc = 0 THEN /* If read was successful */
DO
/*************************************************************/
/* At this point, newvar.0 should be 10, indicating 10 */
/* records have been read. Stem variables newvar.1, newvar.2,*/
/* ... through newvar.10 will contain the 10 records. If we */
/* had not cleared the stem newvar. with the previous DROP */
/* instruction, variables newvar.11 through newvar.20 would */
/* still contain records 11 through 20 from the first data */
/* set. However, we would know that these values were not */
/* read by the last EXECIO DISKR since the current newvar.0 */
/* variable indicates that only 10 records were read by */
/* that last EXECIO. */
/*************************************************************/
Figure 9. EXECIO Example 6 (continued)EXECIO Example 6 (continued)
SAY
SAY
SAY "-----------------------------------------------------"
SAY newvar.0 "records have been read from 'sample2.data': "
SAY
DO i = 1 TO newvar.0 /* Loop through all records */
SAY newvar.i /* Display the ith record */
END
"EXECIO" newvar.0 "DISKW myoutdd (STEM newvar." /* Write
exactly the number of records read */
IF rc = 0 THEN /* If write was successful */
DO
SAY
SAY newvar.0 "records were written to 'all.sample.data'"
END
ELSE
DO
exec_RC = RC /* Save exec return code */
SAY
SAY "Error during 2nd EXECIO …DISKW, return code is " RC
SAY
END
END
ELSE
DO
exec_RC = RC /* Save exec return code */
SAY
SAY "Error during 2nd EXECIO … DISKR, return code is " RC
SAY
END
END
"EXECIO 0 DISKW myoutdd (FINIS" /* Close output file */
"FREE FI(myindd1)"
"FREE FI(myindd2)"
"FREE FI(myoutdd)"
EXIT 0
|