NetRexx Overview, version 2.02
Copyright (c) IBM Corporation, 2001. All rights reserved. ©
22 May 2001
[previous | contents | next]

Expressions and variables

Like say in the ‘toast’ example, many instructions in NetRexx include expressions that will be evaluated. NetRexx provides arithmetic operators (including integer division, remainder, and power operators), several concatenation operators, comparison operators, and logical operators. These can be used in any combination within a NetRexx expression (provided, of course, that the data values are valid for those operations).

All the operators act upon strings of characters (known as NetRexx strings), which may be of any length (typically limited only by the amount of storage available). Quotes (either single or double) are used to indicate literal strings, and are optional if the literal string is just a number. For example, the expressions:

  '2' + '3'

  '2' + 3

    2 + 3

would all result in '5'.

The results of expressions are often assigned to variables, using a conventional assignment syntax:

  var1=5            /* sets var1 to '5'    */

  var2=(var1+2)*10  /* sets var2 to '70' */

You can write the names of variables (and keywords) in whatever mixture of uppercase and lowercase that you prefer; the language is not case-sensitive.

This next sample program, ‘greet’, shows expressions used in various ways:

  /* greet.nrx -- a short program to greet you.        */

  /* First display a prompt:                           */

  say 'Please type your name and then press Enter:'

  answer=ask            /* Get the reply into 'answer' */


  /* If no name was entered, then use a fixed          */

  /* greeting, otherwise echo the name politely.       */

  if answer='' then say 'Hello Stranger!'

               else say 'Hello' answer'!'

After displaying a prompt, the program reads a line of text from the user (‘ask’ is a keyword provided by NetRexx) and assigns it to the variable answer. This is then tested to see if any characters were entered, and different actions are taken accordingly; for example, if the user typed ‘Fred’ in response to the prompt, then the program would display:

  Hello Fred!

As you see, the expression on the last say (display) instruction concatenated the string ‘Hello’ to the value of variable answer with a blank in between them (the blank is here a valid operator, meaning ‘concatenate with blank’). The string ‘!’ is then directly concatenated to the result built up so far. These unobtrusive operators (the blank operator and abuttal) for concatenation are very natural and easy to use, and make building text strings simple and clear.

The layout of instructions is very flexible. In the ‘greet’ example, for instance, the if instruction could be laid out in a number of ways, according to personal preference. Line breaks can be added at either side of the then (or following the else).

In general, instructions are ended by the end of a line. To continue a instruction to a following line, you can use a hyphen (minus sign) just as in English:

  say 'Here we have an expression that is quite long,' -

      'so it is split over two lines'

This acts as though the two lines were all on one line, with the hyphen and any blanks around it being replaced by a single blank. The net result is two strings concatenated together (with a blank in between) and then displayed.

When desired, multiple instructions can be placed on one line with the aid of the semicolon separator:

  if answer='Yes' then do; say 'OK!'; exit; end

(many people find multiple instructions on one line hard to read, but sometimes it is convenient).
[previous | contents | next]

From The NetRexx Language by Mike Cowlishaw, (ISBN 0-13-806332-X, 197pp, Prentice-Hall, 1997).