Keywords: pame

Subsections

- Exercises

# Reference modes in transput

Wherever previously we have used a value of mode `INT` with `print`, we can safely use a name with mode `REF INT`, and similarly with all the other modes (such as `[,]REAL`). This is because the parameters for `print` (the identifiers or denotations used for `print`) are in a firm context and so can be dereferenced before being used.

In the preamble to this chapter, `print`'s counterpart `read` was mentioned. It is now time to examine `read` more closely. Generally speaking, values displayed with `print` can be input with `read`. The main differences are that firstly, the parameters for `read` must be names. For example, we may write

       REF REAL r = LOC REAL;
       read(r)

and the program will skip spaces, tabs and end-of-line and new-page characters until it meets an optional sign followed by optional spaces and at least one digit, when it will expect to read a number. If an integer is present, it will be read, converted to the internal representation of an integer and then widened to a real.

Likewise, `read` may be used to read integers. The plus and minus signs (`+` and `-`) can precede integers and reals. Absence of a sign is taken to mean that the number is positive. Any non-digit will terminate the reading of an integer except for a possible sign at the start. Reals can contain `e` as in `3.41e5`. It is best to ensure that each number is preceded by a sign so that the reading of any preceding number will be terminated by that sign.

For a name of mode `REF CHAR`, a single character will be read, `newline` or `newpage` being called if necessary. In fact, tabs and any other control characters (whose absolute value is less than `ABS blank`) will also be skipped.

If `read` is used to read a `[]CHAR` with fixed bounds as in

       REF[]CHAR sf = LOC[36]CHAR;
       read(sf)

then the number of characters specified by the bounds will be read, `newline` and `newpage` being called as needed. You can call `newline` and `newpage` explicitly to ensure that the next value to be input will start at the beginning of the next line or page.

Just like `print`, `read` can take more than one parameter by enclosing them in a row-display.

You should note that the end of a line or page will not terminate the reading of a number. So if you want to read a number from the keyboard, you should follow the number with a non-digit before pressing “Enter”. In this case, you don't have to read a newline as well, but the “Enter” generates a newline and that newline will be pending in the input.6.7

The only flexible name for which `read` can be used is `REF` `STRING`. When reading values for `REF` `STRING`, the reading pointer will not go past the end of the current line.6.8 If the reading position is already at the end of the line, the row will have no elements. When reading a `STRING`, `newline` must be called explicitly for transput to continue. The characters read are assigned to the name.

## Exercises

5.13
  Write a program to read two real numbers and then print their sum and product. Ans
5.14
  Write a program which will input text line by line (the lines being of different length) and which will then write out each line with the characters reversed. For example, the line `"and so on"` will be displayed as `"no os dna"`. Continue reading until a line of zero length is read. Ans