4.8. Array Constructors

An array constructor is used to specify the value of an array. More precisely, an array constructor is a mechanism that is used to specify a sequence of scalar values that is interpreted as a rank-one array. Syntactically, it is a sequence of scalar values and implied-DO specifications enclosed in parentheses and slashes. As with structures, there is no such thing as an array constant. There are only array constructors, some of which may be constant expressions, as follows:

REAL VECTOR_X(3), VECTOR_Y(2), RESULT(100)
   . . .
RESULT(1:8) = (/ 1.3, 5.6, VECTOR_X, 2.35, VECTOR_Y /)

The value of the first eight elements of RESULT is constructed from the values of VECTOR_X and VECTOR_Y and three real constants in the specified order. If a rank-two or greater array appears in the value list, the values of its elements are taken in array element order. If it is necessary to construct an array of rank greater than one, the RESHAPE(3i) intrinsic function can be applied to an array constructor.

The format for an array_constructor is as follows:

Table 4-21.

 

array_constructor

is

(/ ac_value_list /)

 

ac_value

is

expr

 

 

or

ac_implied_do

 

ac_implied_do

is

(ac_value_list, ac_implied_do_control)

 

ac_implied_do_control

is

ac_do_variable = scalar_int_expr, scalar_int_expr [, scalar_int_expr ]

 

ac_do_variable

is

scalar_int_variable

Each ac_value expression in the array constructor must have the same type, kind type, and length parameters. In particular, this means that if each ac_value is a character literal constant, each constant must have the same length.

The type and type parameters of an array constructor are those of its ac_value expressions.

If the ac_implied_do yields no values, the array is a rank one, zero-sized array.

An ac_do_variable must be a scalar integer named variable. This variable has the scope of this ac_implied_do.

If an ac_implied_do is contained within another ac_implied_do, they must not have the same ac_do_variable.

Three possibilities for an ac_value are as follows:

The possibilities can be mixed in a single array constructor, as follows:

(/ 1.2, B(2:6,:), (REAL(I), I = 1, N), 3.5 /)

If an ac_value is an array expression, the values of the elements of the expression in array element order become the values of the array constructor. For example, the values that result from the example in possibility 2 are as follows:

(/ A(I,1), A(I,2), A(I,3), A(I+1,6), A(I+1,7), A(I+1,8) /)

For more information on array element order, see Section 6.4.7.

If an ac_value is an implied-DO specification, it is expanded to form a sequence of values under control of the ac_do_variable as in the DO construct. For example, the values that result from the example in possibility 3 are as follows:

(/1.0, 1.414, 1.732, 2.0, 2.236, 2.449, 2.645, 2.828, 3.0/)

For more information on the DO construct, see Section 8.6.

If every expression in an array constructor is a constant expression, the array constructor is a constant expression as in the example above. Such an array constructor can be used to assign a value to a named constant, as follows:

REAL X(3), EXTENDED_X(4)
PARAMETER(X = (/ 2.0, 4.0, 6.0 /) )
REAL, PARAMETER :: EXTENDED_X = (/ 0.0, X /) )

The following are examples of array constructors:

Example 1: To create a value for an array of rank greater than one, the RESHAPE(3i) intrinsic function must be used. With this function, a one-dimensional array may be reshaped into any allowable array shape.

Y = RESHAPE(SOURCE = (/ 2.0, (/ 4.5, 4.0 /), Z /),  &
      SHAPE = (/ 3, 2 /))

If Z has the value given in possibility 1, then Y is a 3 by 2 array with the following elements:

2.0     1.2
4.5     3.5
4.0     1.1

Example 2: It might be necessary to construct an array value of derived type.

TYPE PERSON
   INTEGER AGE
   CHARACTER(LEN = 40) NAME
END TYPE PERSON

TYPE(PERSON) CAR_POOL(3)

CAR_POOL = (/ PERSON(35, "SCHMITT"),  &
      PERSON(57, "LOPEZ"), PERSON(26, "YUNG") /)

Example 3: When one of the components of a derived type is an array, then an array constructor must be used in the structure constructor for the derived type. Suppose that the definition for type COLOR is as follows, which differs slightly from that stated previously:

TYPE COLOR
   INTEGER PROPERTIES(3)
   CHARACTER(LEN = 30) NAME
END TYPE COLOR

The following value of the revised type COLOR can be constructed:

COLOR((/ 5, 20, 8 /), "MAGENTA")

Example 4: A derived type might contain an array of rank two or greater, as follows:

TYPE LINE
   REAL     COORD(2, 2)
   REAL     WIDTH
   INTEGER  PATTERN
END TYPE LINE

The values of COORD are the coordinates x1, y1 and x2, y2 representing the end points of a line. WIDTH is the line width in centimeters. PATTERN is 1 for a solid line, 2 for a dashed line, and 3 for a dotted line. An object of type LINE is declared and given a value as follows:

TYPE(LINE) SLOPE
   . . .  
SLOPE = LINE(RESHAPE((/ 0.0, 1.0, 0.0, 2.0 /), (/ 2, 2 /)), 0.1, 1)

The RESHAPE(3i) intrinsic function is used to construct a value that represents a solid line from (0,0) to (1,2) of width 0.1 centimeters.