• Keine Ergebnisse gefunden

Local Variables

Im Dokument MANUAL APL (Seite 34-41)

The fol lowing local variables are named in a function header: result. dummies. and locals. These are 01 I optional; a function is not required to use any local

variables. Notice the fol)owing example:

VR+Y F X;A;B;C

In this example. the function F names the following local variables in its header line:

R (result) - note that R is followed by a + symbol. which designates that R is the result name.

X (dummy) - one name to the right of F separated by blanks(s). designates the right dummy. When F is cal led. the right argument·s value is automatically assigned to

local variable X.

Y (dummy) - one name to the left of F, separated by blank(s). designates the left dummy. When F is called. the left argument·s value is automatically assigned to

local variable Y.

A, B, and C (locals) - note that each local name is preceded by a semicolon.

The remaining type of local variable is the label. Its name appears in a function line as in the example below.

(3) L:Q THIS LINE IS LABELED.

Notice that the label's name. L, fol lows the line number. [3]. and is in turn followed by a colon. Although labels are classified as local variables. it is more appropriate to consider them local constants. They cannot be assigned values; that

is. the fol lowing expression is a syntax error when L is a label:

L+4

The value of a label is the line number of its function line (which cannot change during execution of the function).

The example in Table 3-1 illustrates the effect of shadowing as functions F1 and F2 become active and inactive.

)CLEAR

V-GLOBAL W=GLOBAL X=GLOBAL Y=GLOBAL .... . F2 CALLED ... . F2

V-GLOBAL W-GLOBAL X=GLOBAL Y-GLOBAL .... . Fl CALLED ... . F1

V-GLOBAL W-GLOBAL X-GLOBAL Y=GLOBAL

Define F2. naming W

Arrays and Indexing

As mentioned earlier, a variable may represent a scalar or an array. A scalar is always a single item, an item being a character, number, or nested array. One example of a scalar is:

33

SCLR+33 SCLR

Although an array may be made up of more than one item, it can also consist of a single item or even no items. An array with no items is cal led an empty array.

In addition, arrays can be classified as vectors, matrices, or higher-order arrays.

A vector is an array of one dimension, and is displayed as a collection of items arranged on one I ine. As a typical example, notice the vector named VECT which has four items:

VECT+5 7 9 11 VECT

5 7 9 11

A matrix is an array with two dimensions, (a dimension is sometimes cal led a coordinate) and is displayed as a collection of items arranged in a rectangular pattern. An example of a two-dimensional matrix, named HAT, is shown below:

HAT 1 2 3 4 5

6 7 B 9 10 11 12 13 14 15

Notice that this matrix has three rows and five columns. It is two-dimensional because it is made up of rows and columns.

A higher-order array is an array with three or more dimensions, displayed as a collection of items in a set of rectangular patterns. An example of a higher-order ar ray is:

CUBE 1 2 3 4 5 6 7 B 9 10 11 12 13 14 15 16 17 1B 19 20 21 22 23 24 25 26 27 2B 29 30

This higher-order array is three-dimensional. It has two planes, and each plane has three rows and five columns.

The user can find out if a variable is a scalar, a vector, a matrix, or a

higher-order array by using pp to test for the rank (that is, number of dimensions) of the variable. For example, testing the previous variables SCLR, VECT, HAT, and CUBE wi II give

o

2 3

ppSCLR ppVECT ppHAT ppCUBE

A 0 indicates a scalar, a 1 indicates a vector, a 2 indicates a two-dimensional array, a 3 indicates a three-dimensional array, and so on, up to a maximum of 62 dimensions.

pSCLR pVECT 4 pHAT 3 5

pCUBE

2 3 5

Since a scalar has no dimensions, p of a scalar produces an empty (vector) result;

nothing is displayed (other than the next input prompt). The above example confirms that SCLR is a scalar (no dimension); that VECT is a vector with four items; that HAT is a two-dimensional matrix with three rows and five columns (15 items); and that CUBE is a three-dimensional array with two planes, each with three rows and five columns. One other situation should be noted, p of on empty vector wil I return the value zero, and p of on empty array wil I return one or more zeros depending on which dimension or dimensions have length zero.

Indexing of Arrays

Items in an array can be referenced by their positions within the array. The position number is called an index. The index can also be used for several items.

and to index other indexed arguments. The following topics are discussed in this subsection:

o Referencing a Single Item o Referencing More Than One Item o Assigning a Value to on Array o Indexing an Indexed Argument Referencing a Single Item

An item in an array is referenced by its position within the array. which is

indicated by one or more numbers col led indexes. One number is used as the index of on item in a vector array; two numbers. as the index of on item in a two-dimensional matrix; three numbers. as the index of on item in 0 three-dimensional array; and so on, with one number for each dimension.

The indexes of all arrays start with 0 or 1. depending on the index origin. When the user first enters APL. the index origin is 1 by ~efault. It can be set to 0 by assigning the 010 system variable to O. and reset to 1 by reassigning the 010 variable to 1.

V+'ABCDE' 010+1 V[2J B 010+0

V[2J C

V[lJ B 010+1

The indexes of a two-dimensional matrix also start with 0 or 1. depending on the index origin. but two numbers are used in each index. The first number selects the items from a row. and the second number selects the items from a column. The indexes are ordered with the rightmost position varying the fastest. then the next rightmost.

and so on. For purposes of illustration. consider the matrix named HAT3:

HAT3 3 1 11 2 12 13 15 4 B 14 6 10 7 9 5

The indexes for this matrix. with index origin 1. wil I be example. consider the following three-dimensional array:

HAT4

2;4 5]

8 11 5 6

HAT5(;2 4) 10 8

15 5 3 13

HAT5[1 2 3;2 4) 10 8

15 5 3 13

In fact, the shape of the indexing result has a rank equal to the shape of each of the index expressions joined together. If an index expression is elided, the result shape has the length of the elided coordinate inserted.

Several items in a three-dimensional array are referenced similarly to a matrix, except that the third coordinate must also be added to the index. Consider the fol lowing three-dimensional array:

HAT6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Examples of referencing several items in this array are shown below. These examples assume an index origin of 1.

HAT6[1;2;5J 10

HAT6(;2;) 6 7 8 9 10 16 17 18 19 20

HAT6(;2; 1 3) 6 8

16 18

HAT6( 1 1 2; 1 2 1;1 2 4) 1 2 4

6 7 9 1 2 4 1 2 4 6 7 9 1 2 4 11 12 14 16 17 19 11 12 14

Assigning a Value into an Ar ray

One or more items in an already existing array can be assigned values via the assignment symbol +. The user simply places the variable name and the index designation to the left of the symbol, and the new value to the right. Examples fol low, all of which assume an index origin of 1.

Example of vector:

V+3+t.10

imagine that assignments are "invisible". For example.

3+H(;4)+5

can be analyzed as if the assignment were not present. i.e .•

3+ 5

making the result (8) apparent.

Indexing an Indexed Argument

In APL. an indexed argument may itself be indexed. For example:

Only arguments can be followed by multiple indexes. Specifications and coordinates cannot; thus the following is a syntax error:

A[1;)(2)+X LINESCAN ERR A

Im Dokument MANUAL APL (Seite 34-41)