STANDARD
INPUT AND OUTPUT
 
 

 


There is REXX on AS/400. I asked some of our AS/400 people about it a while back, when I was wandering down the yellow brick road with Dorothy and her miserable little dog, and they'd never heard of it. I pressed F4 and poked around, and I found the Rexx interpreter, but I eventually decided that God wanted me to FTP the files I was interested in to a Unix machine and play around with them there.

PATRICK TJ MCPHEE: subject Re: AS/400 in comp.lang.rexx 1999/02/05

The parse linein instruction reads from the only STDIN file and the say instruction writes to the only STDOUT file. You can redirect these files before the Rexx program uses ones - but Only this and nothing more. I show you the first possibility how we can overcome this restriction.

Programs in the Rexx language are always interpret. Either in interactive mode (see the example in previous chapter) or batch mode. A Rexx program has access to two files for standard input and output operations. The standard input stream STDIN is available for an input; the standard output stream STDOUT is available for an output. A second output file, STDERR, is used by the interpreter to write error messages and trace information. The parse linein instruction reads from the STDIN file and the say instruction writes to the STDOUT file.

Default STDIN in the interactive mode is keyboard; defalut STDOUT in interactive mode is a screen. In batch mode the default STDIN is the QINLINE file and the default STDOUT is the QPRINT spool file.

You can redirect both STDIN and STDOUT, by using the CL override commands (name of this commands begins OVR...), in order to work with other than the defaults. If you use an override command for this purpose, the overridden file name must be specified as STDIN or STDOUT. Override commands must be issued before the Rexx program uses these files.

Now I will demonstrate as the power features of Rexx and AS/400 can join for the simple solution of a complicated task.

Our goal - a program, which for given document discovers how many times each word occur, where word means a sequence of characters that do not include any blanks. A document is saved into the first member of the INFO physical file in the MYLIB library. The following HOWMANY program provides the answer:


/* HOWMANY */
Count = 0
'RTVMBRD FILE(MYLIB/INFO) MBR(*FIRST) NBRCURRCD(&COUNT)'
'OVRDBF FILE(STDIN) TOFILE(MYLIB/INFO)'

Number. = 0; Various = ""
do Count
  parse linein Record
  do J = 1 to WORDS(Record)
    Oneword = WORD(Record, J)
    if Number.Oneword = 0 then Various = Various Oneword
    Number.Oneword = Number.Oneword + 1
  end
end
do J = 1 to WORDS(Various)
  Oneword = WORD(Various, J)
  say Number.Oneword Oneword
end

The program shows use of Rexx's associative arrays and CL commands: the RTVMBRD (Retrieve Member Description) command returns in the Count variable the number of records of an input file. We say this variable pseudo-CL variable. The variable name must be immediately preceded by an ampersand (&). The OVRDBF (Override with Data Base File) command redirects STDIN to the INFO file in the MYLIB library. In interactive mode you will see a result on the screen. When you want see the result on a paper you must add as a first clause the

'OVRPRTF FILE(STDOUT) TOFILE(QSYSPRT)'

command. Another solution is pass the program to batch processing by the help of the SBMJOB (Submit Job) command:

SBMJOB CMD(STRREXPRC SRCMBR(HOWMANY) SRCFILE(MYLIB/QREXSRC))

The HOWMANY program can be usefull in practice when the result will sorted. How to do it most simple? Jon Bentley wrote in his Programming Pearls: Work with the Environment and Use the Right Tool for the job. A most simple solution in OS/400 is put the result's data into a file and running a query for this file. Query/400 is an easy-to-use report generator that lets extract information from database files. I show you creating and using query again step by step.

Step 1

First create file OUTPUT by the help of the command written at the command line:

CRTPF MYLIB/OUTPUT RCDLEN(80)

Now create a query for the OUTPUT file. You can use the WRKQRY command and press Enter. Fill out the form as the following picture shows and press Enter.


                               Work with Queries 
Type choices, press Enter.
  Option  . . . . . .    1              1=Create, 2=Change, 3=                                         5=Display, 6=Print def                                         8=Run in batch, 9=Run   Query . . . . . . .    SAYWORD        Name, F4 for list     Library . . . . .      MYLIB        Name, *LIBL, F4 for li

Step 2

Press the Enter key on the Define the Query display with default values 1 (i.e. Select) in the option column in front of Specify file selections. On the following Specify File Selection display type the name of a file (OUTPUT) and the name of a library (MYLIB). Press the Enter key to confirm.


                           Specify File Selections 
Type choices, press Enter.  Press F9 to specify an additional   file selection.
  File . . . . . . . . .   OUTPUT         Name, F4 for      Library  . . . . . .     MYLIB        Name, *LIBL, F4 for   Member . . . . . . . .   *FIRST         Name, *FIRST, F4 for   Format . . . . . . . .   *FIRST         Name, *FIRST, F4 for

You return to the Define the Query display. Type 1 in the option field in front of Select sort fields and press the Enter key. On the Select Sort Fields display, as shown below, you select the sort fields that you want by entering a sort priority number in the Sort Prty column. You can also specify whether you want the sort field sorted in ascending order (from lowest to highest value) or descending order (from highest to lowest value) by typing an A (for ascending) or a D (for descending) in the A/D column.


                           Select Sort Fields 
Type sort priority (0-999) and A (Ascending) or D (Descendin   the names of up to 32 fields, press Enter.
Sort
Prty A/D Field         Text

 1    D  OUTPUT                                                

Press the Enter key to confirm. You go back on the Define the Query display. Press the F3 key. On the following Exit this Query display move your cursor to the Run option field and type 3 (Do not run). Press the Enter key to save your query definition. And press F3 at the end work with query.

Step 3

Change the HOWMANY program so, that couple the number of occurence and word will saved as the record into the OUTPUT file. For easy sorting use the instruction:

say FORMAT(Number.Oneword, 5) Oneword

Add to the HOWMANY program the CLRPFM (Clear Physical File Member) command and the RUNQRY command to run the SAYWORD query.


/* HOWMANY */
'CLRPFM FILE(MYLIB/OUTPUT)'
'OVRDBF FILE(STDOUT) TOFILE(MYLIB/OUTPUT)'
Count = 0
'RTVMBRD FILE(MYLIB/INFO) MBR(*FIRST) NBRCURRCD(&COUNT)'
'OVRDBF FILE(STDIN) TOFILE(MYLIB/INFO)'

Number. = 0; Various = ""
do Count
  parse linein Record
  do J = 1 to WORDS(Record)
    Oneword = WORD(Record, J)
    if Number.Oneword = 0 then Various = Various Oneword
    Number.Oneword = Number.Oneword + 1
  end
end
do J = 1 to WORDS(Various)
  Oneword = WORD(Various, J)
  say FORMAT(Number.Oneword, 5) Oneword
end
'RUNQRY QRY(MYLIB/SAYWORD)'

If you want to change anything about the output device - the output can be displayed, printed, or stored in another database file - you must first change the query and then run it. Do you see? The output of Rexx program was redirected to the file OUTPUT by the 'OVRDBF FILE(STDOUT) ...' command, but you can read results on the screen!


                                Display Report
Query . . . :   MYLIB/SAYWORD                Report width . . . Position to line  . . . . .              Shift to column  . . . Line   ....+....1....+....2....+....3....+....4....+....5....+.        OUTPUT
000001
    66 .
000002    10 *
000003     7 :
000004     2 1


tour from Rexx to AS/400

 [How write and run Rexx program]

 [Standard Input and Output]

 [External Data Queue]

 [SQL statements]


main page rexx page apple 
snails identification and authentication optical illusions mail ceska verze

last modified 26th April 2002
Copyright © 1998-2002 Vladimir Zabrodsky
Czech Republic