 |
» |
|
|
 |
An array constructor is
used to assign values to an array. The generated values are supplied
from a list of scalar values, arrays of any rank, and implied DO specifications. An array constructor may appear
in any context in which a rank-one array expression is allowed. An
array with a rank greater than one may be constructed by using the RESHAPE intrinsic function. The type of an array constructor
is taken from the values in the list, which must all have the same
type and type parameters (including character length). The extent
is taken from the number of values specified. The syntax of
an array constructor is: (/ ac-value-list /) where ac-value-list is a comma-separated list of one or more ac-values. Each ac-value may be any of the following: Scalar expressions, for example: An array expression, for example: where the values in x(0) through x(5) become the values of the array constructor. If
the array the value list has a rank greater than one, the values
are generated in column-major order, as explained in “Array
fundamentals”. An implied-DO specification, taking the form: (ac-value-list, do-var = expr1, expr2 [, expr3]) where do-var is the name of a scalar integer variable, expr1 is the initial value, expr2 is the final value, and expr2 is the stride (the default is 1). For example:
When used to initialize
an array in a type declaration or in an assignment statement, all elements
in the array must be initialized. For example, the following is
illegal: INTEGER :: i(10) = (/ 1, 2, 3 /) ! ILLEGAL: too few ! initializers
|
If no values are supplied, the array constructor is zero-sized.
For example, the size of the following array constructor: depends on the value of the variable n; if the value of the variable is less than 10,
then the constructor contains no values. If the list
contains only constant values, the array constructor may initialize
a named constant or a type declaration statement. An array constructor
may not initialize variables in a DATA statement, which may contain scalar constants
only. As
an extension, HP Fortran allows the use of [ and ] in place of (/ and /). The following are examples
of array constructors: ! array x is assigned three real values. x = (/19.3, 24.1, 28.6/) ! One vector, consisting of 16 integer values, is assigned to j j = (/4, 10, k(1:5), 2 + l, (m(n), n = -7,-2),16, 1/) ! assign 5 values a = (/(base(k), k=1,5)/) ! The named constant t is a rank-one array initialized with ! the values 36.0 and 37.0 REAL,DIMENSION(2):: t PARAMETER (t=(/ 36.0, 37.0/)) ! the array constructor is reshaped as 1 3 5 7 ! 2 4 6 8 ! and is then assigned to z z=RESHAPE((/1,2,3,4,5,6,7,8/), (/2,4/) ) ! an array constructor is used for the second component of ! the structure constructor alaska = site(”NOME”,(/-63,4/)) diagonal = (/ (b(i,i), i=1,n) /) hilbert = RESHAPE( (/ ((1.0/(i+j), i=1,n), j=1,n) /), (/ n,n /) ) ident = RESHAPE ( (/ (1, (0, i=1,n), j=1,n-1), 1 /), (/ n,n /) ) |
As shown in
last three examples, an array constructor with implied- DO loops and the RESHAPE function permit construction of arrays that cannot
otherwise be expressed conveniently with alternative notations.
|