Fortran Arrays Array constructors


Example

A rank-1 array value can be created using an array constructor, with the syntax

(/ ... /)
[ ... ]

The form [...] was introduced in Fortran 2003 and is generally regarded as clearer to read, especially in complex expressions. This form is used exclusively in this example.

The values featuring in an array constructor may be scalar values, array values, or implied-do loops.

The type and type parameters of the constructed array match those of the values in the array constructor

[1, 2, 3]      ! A rank-1 length-3 array of default integer type
[1., 2., 3.]   ! A rank-1 length-3 array of default real type
["A", "B"]     ! A rank-1 length-2 array of default character type

integer, parameter :: A = [2, 4]
[1, A, 3]      ! A rank-1 length-4 array of default integer type, with A's elements

integer i
[1, (i, i=2, 5), 6]  ! A rank-1 length-6 array of default integer type with an implied-do

In the forms above, all the values given must be of the same type and type parameter. Mixing types, or type parameters, is not allowed. The following examples are not valid

[1, 2.]      ! INVALID: Mixing integer and default real
[1e0, 2d0]   ! INVALID: Mixing default real and double precision
[1., 2._dp]  ! INVALID: Allowed only if kind `dp` corresponds to default real
["Hello", "Frederick"]  ! INVALID: Different length parameters

To construct an array using different types, a type specification for the array shall be given

[integer :: 1, 2., 3d0]    ! A default integer array
[real(dp) :: 1, 2, 3._sp]  ! A real(dp) array
[character(len=9) :: "Hello", "Frederick"]  ! A length-2 array of length-9 characters

This latter form for character arrays is especially convenient to avoid space padding, such as the alternative

["Hello    ", "Frederick"]  ! A length-2 array of length-9 characters

The size of an array named constant may be implied by the array constructor used to set its value

integer, parameter :: ids(*) = [1, 2, 3, 4]

and for length-parameterized types the length parameter may be assumed

character(len=*), parameter :: names(*) = [character(3) :: "Me", "You", "Her"]

The type specification is also required in the construction of zero-length arrays. From

[ ] ! Not a valid array constructor

the type and type parameters cannot be determined from the non-existing value set. To create a zero-length default integer array:

[integer :: ]

Array constructors construct only rank-1 arrays. At times, such as in setting the value of a named constant, higher rank arrays are also required in an expression. Higher rank arrays can be taken from the result of reshape with a constructed rank-1 array

integer, parameter :: multi_rank_ids(2,2) = RESHAPE([1,2,3,4], shape=[2,2])

In an array constructor the values of the array in element order with any arrays in the value list being as though the individual elemets were given themselves in array element order. Thus, the earlier example

integer, parameter :: A = [2, 4]
[1, A, 3]      ! A rank-1 length-4 array of default integer type, with A's elements

is equivalent to

[1, 2, 4, 3]   ! With the array written out in array element order

Generally the values in the constructor may be arbitrary expressions, including nested array constructors. For such an array constructor to meet certain conditions, such as being a constant or specification expression, restrictions apply to constituent values.


Although not an array constructor, certain array values may also be conveniently created using the spread intrinsic function. For example

[(0, i=1,10)]  ! An array with 10 default integers each of value 0

is also the result of the function reference

SPREAD(0, 1, 10)