INPUT Instruction |
INPUT( options ) arrays, variables, or array elements
INPUT reads data into arrays and variables. It is strictly free-format (fields separated by blanks, commas, tabs or end-of-lines) and you can use it for all types of numeric or character variables. By contrast, READ has a FORMAT option and can handle many types of data files. The FIXED instruction is useful for setting arrays of fixed values in PROCEDUREs and FUNCTIONs.
Options
UNIT=[INPUT]/DATA/other unit
INPUT reads the data from the specified RATS I/O unit. The INPUT unit (the default setting) is simply the console if you are working interactively or the current input file if you are working in batch mode or have done a SOURCE or OPEN INPUT instruction.
VARYING/[NOVARYING]
SINGLELINE]/NOSINGLELINE
STATUS=INTEGER variable set to 0,1 status
The VARYING and STATUS options allow you to work with lists whose size you do not want to set in advance.
You can use VARYING to input data for a single VECTOR array of any numeric or character type. With VARYING, the VECTOR is filled with as much data as is available. By default, this is whatever is on a single line of data.
With NOSINGLELINE, it will read data until the end of the file—though READ is preferable for data coming from a file.
If you use the option STATUS, INPUT does not give you an error if there is not enough data to fill all the variables. Instead, it sets your status variable to 0. If the INPUT is successful, it sets the status variable to 1.
Description
Some general information about INPUT:
•It reads the arrays and variables in the order listed.
•It fills the elements of arrays in the order described below.
•It can read two or more arrays from the same line of data. It can read two or more rows of an array from a single line.
•It reads complex numbers as a pair of real numbers: the real and imaginary parts, respectively.
•A STRING variable is filled with the contents of a complete line.
Before you can INPUT data for a variable, you must first introduce it with DECLARE or some other instruction. You also need to dimension any array prior to using it in an INPUT instruction, unless you use the VARYING option.
Organization of Arrays
These are the three general forms of arrays and their organization:
One-dimensional arrays (VECTOR array type)
are written and read as row vectors.
General two-dimensional arrays (RECTANGULAR array type)
are written and read by rows (the natural fashion), even though internally they are stored by columns.
Specialized two-dimensional arrays (SYMMETRIC and PACKED array types)
are written and read by rows of the lower triangle of the matrix. Thus, the first row has one column, the second has two, etc.
Examples
dec rect[integer] laglengths(4,4)
input laglengths
4 3 1 4
2 4 4 4
1 4 4 4
1 2 4 4
dec rect lr(5,5) sr(5,5)
input lr
. . . . .
. . . . .
. . . . .
. . . . 0
. . . . .
input sr
. 0 0 0 0
. . 0 0 0
. . . 0 0
. . . . .
. . . . .
The .'s translate to missing values.
dec vect[strings] countries(19)
input countries
Austria
Belgium
Denmark
Finland
France
Germany
Iceland
Ireland
Netherlands
Norway
Portugal
Spain
Switzerland
UK
Japan
Australia
New Zealand
Canada
USA
Note that the input strings don't have quotes.
dec vector v
input(varying) v
1 5 10 25 50 100 250 500 1000
VARYING is useful if you would rather not count the entries or want to make quick changes to the list without having to worry about changing the dimensions.
declare symmetric v(3,3)
declare real y
declare vector[complex] c2(2)
declare integer i
input v y c2 i
1.0 2.0 3.0 4.0 5.0 6.0 7.3
0.0 1.0 1.0 0.0 5
sets all of the following:
v = \(\left[ {\begin{array}{*{20}{c}}{1.0} & {} & {} \\{2.0} & {3.0} & {} \\{4.0} & {5.0} & {6.0} \\\end{array}} \right]\)
Y = 7.3
C = \(\left[ {\begin{array}{*{20}{c}}{0.0 + 1.0i} \\{1.0 + 0.0i} \\\end{array}} \right]\)
I = 5
You can also use COMPUTE for the initialization above:
compute [symmetric] v=||1.0|2.0,3.0|4.0,5.0,6.0||
compute y=7.3
compute [vector[complex]] c2=||%cmplx(0.0,1.0),%cmplx(1.0,0.0)||
compute i=5
We prefer to use COMPUTE for integer and real scalars and vectors. For arrays with multiple rows, however, INPUT is easier to read and requires fewer extra characters. Compare the initializer for V above with
dec symm v(3,3)
input v
1.0
2.0 3.0
4.0 5.0 6.0
Of course, INPUT is only useful when you are supplying explicit values. If you want to set an array using variable names or other expressions, use COMPUTE instead.
Copyright © 2026 Thomas A. Doan