Introductory Rexx Tutorial

Источник: The Rexx Language Association

Дата: Spring 1997


These files allow you to review the topics covered in the Introductory Rexx Tutorial presented at the SHARE Technology Conference 2-7 March 1997.

These files are a supplement to the discussion in that session and are intended as a refresher for the topics covered there. The refresher course is automated as a series of Rexx programs, which are generally portable to any platform.

These sample programs use basic elements of the Rexx language and most of them require no modification to run in any operating system environment. The exceptions are those programs that interact with the operating system. To use these programs in your home environment, please review the notes that follow:

If you are running under OS/2, no changes are required.

For all other platforms, you will need to make the following changes:

    The file names are suitable for an OS/2, DOS, or Windows environment. When copying the files to another platform, be sure to choose appropriate names for Rexx programs on that platform.
    The files addr1.cmd, addr2.cmd, and addr3.cmd include a command to produce a directory listing under OS/2 or DOS ("dir" or "dir /w"). You must change this to a command that is valid for your operating system. Any valid operating system command will illustrate the points being made in these programs.

The files are identified below.
FIRST.CMD     simplest Rexx program - how to do output
NEXT.CMD     simple but better Rexx program - output again
       
PROMPT1.CMD     getting fancier - how to get input and
PROMPT2.CMD     variations on input and output
PROMPT3.CMD    
       
GREET1.CMD     simplest loop
GREET2.CMD     more useful looping
       
NUMBERS.CMD     how numbers are treated in Rexx
       
DATATYPE.CMD     determining what kind of string this is - validating input
DATATYP2.CMD    
CONTINUE.CMD     continuation of lines in a program
CONTINU2.CMD    
       
PRECISE.CMD     Rexx's decimal arithmetic
       
FUNCS.CMD     using functions - and some useful functions
ADDR1.CMD     interfacing with the external environment, also
ADDR2.CMD     illustrating some potential pitfalls in Rexx and
ADDR3.CMD     how to avoid them
       
MAGICNO.CMD     a self-documenting Rexx program, though all good programmers document their code

FIRST.CMD

/*  FIRST.CMD  */
/*  My first REXX program  */
say Hello world

NEXT.CMD

/*  NEXT.CMD  */
/*  My next REXX program  */
say 'Hello world'

PROMPT1.CMD

/*  PROMPT1.CMD  */
/*  Prompt sequence example  */
say 'Please type your name'
pull reply
say 'Hello' reply

PROMPT2.CMD

/*  PROMPT2.CMD  */
/*  Case-sensitive prompt sequence example  */
say 'Please type your name'
parse pull reply
say 'Hello' reply

PROMPT3.CMD

/*  PROMPT3.CMD  */
/*  "Prettier" and fancier prompt sequence example  */
say ''
say 'Please type your name'
say ''
parse pull name
say ''
say 'Hello' name
say 'Welcome to the Intro REXX Tutorial!'

GREET1.CMD

/*  GREET1.CMD  */
/*  Greetings  */
do 3
  say ''
  say 'Hi there'
end

GREET2.CMD

/*  GREET2.CMD  */
/*  Polite greetings  */
say ''
say 'How many times would you like me to greet you?'
/*  ask user  */
pull /*  get /*  why is this comment here?  */ from terminal  */ howmany
do i = 1 to howmany
  say ''
  say 'Hi there'
end

NUMBERS.CMD

/*  NUMBERS.CMD  */
/*  Numbers in REXX - just strings until you need to do arithmetic - then they are treated as numbers  */
x = 256
y = 8
say ''
say 'x is' x
say 'Leftmost character of x is' left(x, 1)
say ''
say 'y is' y
say 'Length of y is' length(y)
say ''
say 'x divided by y is' x/y

DATATYPE.CMD

/*  DATATYPE.CMD  */
/*  Even though everything is a string in REXX, it is sometimes useful to know what sort of string we have
    - particularly if we want to attempt to validate user response to a prompt  */
say ''
say 'Introducing "DATATYPE()"'
say ''
say 'Type a number or a letter'
say ''
pull reply
say ''
if datatype(reply) = 'NUM' then say 'You entered a number'
else say 'You entered a letter'

DATATYP2.CMD

/*  DATATYP2.CMD  */
/*  Even though everything is a string in REXX, it is sometimes useful to know what sort of string we have
    - particularly if we want to attempt to validate user response to a prompt  */
say ''
say 'Introducing "DATATYPE()"'
say ''
say 'Type a number'
say ''
pull reply
say ''
if \datatype(reply, 'w') then say 'That was not a whole number'
else say 'That was a whole number'

CONTINUE.CMD

/*  CONTINUE.CMD  */
/*  Continuation is REXX is easy. Suppose we want to create a really long string but we want to see everything
    in our editor without having to do left/right scrolling  */
string = 'January February March April May June July August',
  'September October November December'
say ''
say string

CONTINU2.CMD

/*  CONTINU2.CMD  */
/*  Continuation is REXX is easy. You can continue any clause  */
say ''
say 'Please type a number between 1 and 10'
say ''
pull reply
if \datatype(reply, 'w') | reply < 1,
  | reply > 10 then say 'You didn't follow my instructions'
else say "Congratulations! You're paying attention"

PRECISE.CMD

/*  PRECISE.CMD  */
/*  Illustration of numeric precision  */
arg precision
if precision \= '' then numeric digits precision
say 4/7

FUNCS.CMD

/*  FUNCS.CMD  */
/*  Some very useful functions  */
say ''
say 'Various date formats'
say ''
say '"Normal":' date() 'normal in the US, at least'
say '"Ordered":' date('o') 'good when you need to sort by date'
say '"Sorted":' date('s') 'possibly even better for sorting'
say '"Base":' date('b') 'necessary for date calculations'
say '"Weekday":' date('w') 'self explanatory'
say '"Month":' date('m')
say ''
say ''
say 'Time formats'
say ''
say '"Normal":' time() 'hours, minutes, and seconds'
say '"Civil":' time('c') 'if you want am or pm'
say ''
say 'And there are options to get the hours, minutes, or seconds since'
say 'midnight, as well as an "elapsed time clock" which may be'
say 'reset within the program for internal timings'

ADDR1.CMD

/*  ADDR1.CMD  */
/*  REXX interacts well and easily with its environment. If an expression does not evaluate to a valid REXX
    clause, the result is automatically send to the external environment to be handled  */
dir

ADDR2.CMD

/*  ADDR2.CMD  */
/*  REXX interacts well and easily with its environment. If an expression does not evaluate to a valid REXX
    clause, the result is automatically send to the external environment to be handled. But some care is in
    order. If uninitialized, "dir" evaluates to "DIR". This is not a REXX clause so is sent to the OS to handle.
    What if, somewhere else in your program, you tried to use "dir" as a variable name?  */
say ''
dir = 'C:\RXTUTOR\SHARE\INTRO2'
say ''
say 'Some code gets executed here, and then .....'
say ''
dir

ADDR3.CMD

/*  ADDR3.CMD  */
/*  REXX interacts well and easily with its environment. If an expression does not evaluate to a valid REXX
    clause, the result is automatically send to the external environment to be handled. But some care is in
    order. If uninitialized, "dir" evaluates to "DIR". This is not a REXX clause so is sent to the OS to handle.
    What if, somewhere else in your program, you tried to use "dir" as a variable name? So it's always safe ...
    and generally considered good programming practice to enclose commands for the external environment in quotes.
    This ensures they are literal strings and not evaluated by REXX before execution. It also allows for the use of
    characters that would be considered by REXX to be an operator.  */
say ''
dir = 'C:\RXTUTOR\SHARE\INTRO2'
say ''
say 'Some code gets executed here, and then .....'
say ''
'dir /w'
   

MAGICNO.CMD

/*  MAGICNO.CMD  */
/*  Self-documenting program  */
secret_number = random(1, 10)
guessed_it = 0
say 'I''m thinking of a number between 1 and 10'
say 'Let''s see if you can guess it'
do until guessed_it
  say ''
  say 'Pick a number or type "Q" if you give up'
  pull guess
  if guess = 'Q' then leave
  if guess = secret_number then guessed_it = 1
  else say 'Sorry, that''s not it'
end
say ''
if guessed_it then say 'Hey, that''s right! The number I was thinking of was' secret_number
say 'Thanks for playing my game'