An array constructor is a sequence of specified scalar values. It constructs a rank-one array whose element values are those specified in the sequence.
>>-(/--ac_value_list--/)--------------------------------------->< |
If ac_value is:
The data type of the array constructor is the same as the data type of the ac_value_list expressions. If every expression in an array constructor is a constant expression, the array constructor is a constant expression.
You can construct arrays of rank greater than one using an intrinsic function. See RESHAPE(SOURCE, SHAPE, PAD, ORDER) for details.
INTEGER, DIMENSION(5) :: A, B, C, D(2,2) A = (/ 1,2,3,4,5 /) ! Assign values to all elements in A A(3:5) = (/ 0,1,0 /) ! Assign values to some elements C = MERGE (A, B, (/ T,F,T,T,F /)) ! Construct temporary logical mask ! The array constructor produces a rank-one array, which ! is turned into a 2x2 array that can be assigned to D. D = RESHAPE( SOURCE = (/ 1,2,1,2 /), SHAPE = (/ 2,2 /) ) ! Here, the constructor linearizes the elements of D in ! array-element order into a one-dimensional result. PRINT *, A( (/ D /) )
Implied-DO loops in array constructors help to create a regular or cyclic sequence of values, to avoid specifying each element individually.
A zero-sized array of rank one is formed if the sequence of values generated by the loop is empty.
>>-(--ac_value_list--,--implied_do_variable-- = --expr1--,--expr2--> >----+-----------+--)------------------------------------------>< '-,--expr3--' |
The variable has the scope of the implied-DO, and it must not have the same name as another implied-DO variable in a containing array constructor implied-DO:
M = 0 PRINT *, (/ (M, M=1, 10) /) ! Array constructor implied-DO PRINT *, M ! M still 0 afterwards PRINT *, (M, M=1, 10) ! Non-array-constructor implied-DO PRINT *, M ! This one goes to 11 PRINT *, (/ ((M, M=1, 5), N=1, 3) /) ! The result is a 15-element, one-dimensional array. ! The inner loop cannot use N as its variable.
PRINT *, (/ (I, I = 1, 3) /) ! Sequence is (1, 2, 3) PRINT *, (/ (I, I = 1, 10, 2) /) ! Sequence is (1, 3, 5, 7, 9) PRINT *, (/ (I, I+1, I+2, I = 1, 3) /) ! Sequence is (1, 2, 3, 2, 3, 4, 3, 4, 5) PRINT *, (/ ( (I, I = 1, 3), J = 1, 3 ) /) ! Sequence is (1, 2, 3, 1, 2, 3, 1, 2, 3) PRINT *, (/ ( (I, I = 1, J), J = 1, 3 ) /) ! Sequence is (1, 1, 2, 1, 2, 3) PRINT *, (/2,3,(I, I+1, I = 5, 8)/) ! Sequence is (2, 3, 5, 6, 6, 7, 7, 8, 8, 9). ! The values in the implied-DO loop before ! I=5 are calculated for each iteration of the loop.