MainframeSupports
tip week 43/2007:

Throughout the years I have often used the TSO/REXX command EXECIO in my tips. I feel it is about time I explain this command for you because it is often used to read or write one record at a time. That is indeed a waste of time since it is much faster to use a stem for input or output.

The traditional way of using EXECIO may look like this:

ADDRESS TSO "ALLOC FI(MYFILE) DA('MYUSER.DATASET') SHR REUSE"
ADDRESS TSO "EXECIO 0 DISKR MYFILE (OPEN"
ADDRESS TSO "EXECIO 1 DISKR MYFILE"
DO WHILE RC = 0
  PULL MYRECORD
  /* Process contens of variable myrecord */
  ADDRESS TSO "EXECIO 1 DISKR MYFILE"
END
ADDRESS TSO "EXECIO 0 DISKR MYFILE (FINIS"
ADDRESS TSO "FREE FI(MYFILE)"

The first EXECIO opens the file allocated to DD name MYFILE. The next EXECIO reads the first record from the file and into the REXX stack. The REXX statement PULL fetches the last record put onto the REXX stack into a REXX variable. The last EXECIO closes the file. It is always adviceable to close open files as you may get into a lot of trouble by not doing so. The piece of code is just like how you will do it in PL/I or COBOL. That is why many REXX programs look like the example. It is much faster both to code and to execute the same functionality in the following way:

DROP MYRECORDS.
ADDRESS TSO "ALLOC FI(MYFILE) DA('MYUSER.MYINPUT.DATASET') SHR REUSE"
ADDRESS TSO "EXECIO * DISKR MYFILE (STEM MYRECORDS. OPEN FINIS"
ADDRESS TSO "FREE FI(MYFILE)"
DO RECORDNO = 1 TO MYRECORDS.0
  MYRECORD = MYRECORDS.RECORDNO
  /* Process contens of variable myrecord */
END

The first and only EXECIO opens the file allocated to DD name MYFILE, reads the whole content into the stem variable MYRECORDS. and closes the file afterwards. After successful execution of EXECIO the variable MYRECORDS.0 contains the number of records read. If MYRECORDS.0 contains 0 the file was empty. The content of the first record in the file is present in variable MYRECORDS.1 and so on. It is a simple as that. Please notice the DROP MYRECORDS. statement which initializes the stem variable. It is a very good idea to use DROP before EXECIO if you read into the same stem variable more than once in the same program.

If you want to write the content of stem variable MYRECORDS. into a file all you have to do is to change DISKR to DISKW in the EXECIO command and nothing else. The complete content of the stem variable will be written to the file and not just the number of records indicated by MYRECORDS.0. This detail may cause a lot of confusion. Therefore it is a very good idea to use the DROP statement on your stem variable before filling data into it.

The amount of records held in one stem variable is of course limited by the amount of virtual storage made available for your TSO session. Therefore it is not always the best idea to use EXECIO * for DISKR. * indicates that all records in the file must be processed. Instead you may replace * with the number of records you want to read into your stem variable. By combining the two examples you will be able to process large files using REXX with a reasonable performance. Last but not least you get a link to the TSO/E REXX reference about EXECIO. You can do a lot of stuff with this command.

Previous tip in english        Sidste danske tip        Tip list