3.4.2.4 Array Constructors

An array constructor can be used to create and assign values to rank-one arrays (and array constants). An array constructor takes the following form:

(/ac-value-list/)

ac-value-list
Is a list of one or more expressions or implied-do loops. Each ac-value must have the same type and kind parameters, and be separated by commas.

An implied-do loop in an array constructor takes the following form:

(ac-value-list, do-variable = expr1, expr2 [, expr3])

do-variable
Is the name of a scalar integer variable. Its scope is that of the implied-do loop.

expr
Is a scalar integer expression. The expr1 and expr2 specify a range of values for the loop; expr3 specifies the stride. The expr3 must be a nonzero value; if it is omitted, it is assumed to be 1.

Rules and Behavior

The array constructed has the same type as the ac-value-list expressions.

If the sequence of values specified by the array constructor is empty (there are no expressions or the implied-do loop produces no values), the rank-one array has a size of zero.

An ac-value is interpreted as follows:

Form of ac-value  Result 
A scalar expression  Its value is an element of the new array. 
An array expression  The values of the elements in the expression (in array element order) are the corresponding sequence of elements in the new array. 
An implied-do loop  It is expanded to form a list of array elements under control of the DO variable (like a DO construct). 

The following shows the three forms of an ac-value:

C1 = (/4,8,7,6/)                  ! A scalar expression
C2 = (/B(I, 1:5), B(I:J, 7:9)/)   ! An array expression
C3 = (/(I, I=1, 4)/)              ! An implied-do loop

You can also mix these forms, for example:

C4 = (/4, A(1:5), (I, I=1, 4), 7/)

If every expression in an array constructor is a constant expression, the array constructor is a constant expression.

If the expressions are of type character, Fortran 95/90 requires each expression to have the same character length.

However, Compaq Fortran allows the character expressions to be of different character lengths. The length of the resultant character array is the maximum of the lengths of the individual character expressions. For example:

print *,len ( (/'a','ab','abc','d'/) )
print *,'++'//(/'a','ab','abc','d'/)//'--'

This causes the following to be displayed:

           3
 ++a  --++ab --++abc--++d  --

If an implied-do loop is contained within another implied-do loop (nested), they cannot have the same DO variable (do-variable).

To define arrays of more than one dimension, use the RESHAPE intrinsic function.

The following are alternative forms for array constructors:

Examples

The following example shows an array constructor using an implied-do loop:

INTEGER ARRAY_C(10)
ARRAY_C = (/(I, I=30, 48, 2)/)

The values of ARRAY_C are the even numbers 30 through 48.

The following example shows an array constructor of derived type that uses a structure constructor:

TYPE EMPLOYEE
  INTEGER ID
  CHARACTER(LEN=30) NAME
END TYPE EMPLOYEE

TYPE(EMPLOYEE) CC_4T(4)
CC_4T = (/EMPLOYEE(2732,"JONES"), EMPLOYEE(0217,"LEE"),     &
          EMPLOYEE(1889,"RYAN"), EMPLOYEE(4339,"EMERSON")/)

The following example shows how the RESHAPE intrinsic function can be used to create a multidimensional array:

E = (/2.3, 4.7, 6.6/)
D = RESHAPE(SOURCE = (/3.5, (/2.0, 1.0/), E/), SHAPE = (/2,3/))

D is a rank-two array with shape (2,3) containing the following elements:

   3.5    1.0    4.7
   2.0    2.3    6.6

For More Information:


Previous Page Next Page Table of Contents