LF Fortran 95

Array References

A whole array is referenced by the name of the array. Individual elements or sections of an array are referenced using array subscripts.


array [(subscript-list)]


array is the name of the array.

subscript-list is a comma-separated list of

element-subscript or subscript-triplet or vector-subscript

element-subscript is a scalar INTEGER expression.

subscript-triplet is [element-subscript]:[element-subscript][:stride]

stride is a scalar INTEGER expression.

vector-subscript is a rank one INTEGER array expression.

The subscripts in subscript-list each refer to a dimension of the array. The left-most subscript refers to the first dimension of the array.

Array Elements

If each subscript in an array subscript list is an element subscript, then the array reference is to a single array element. Otherwise, it is to an array section (see See Array Sections).

Array Element Order

The elements of an array form a sequence known as array element order. The position of an element of an array in the sequence is:

(1 + (s1 - j1)) + ((s2 - j2) * d1) + ... ((sn - jn) * dn-1 * dn-2 ... * d1)


si is the subscript in the ith dimension.

ji is the lower bound of the ith dimension.

di is the size of the ith dimension.

n is the rank of the array.

Another way of describing array element order is that the subscript of the leftmost dimension changes most rapidly as one goes from first element to last in array element order. For example, in the following code

integer, dimension(2,3) :: a

the order of the elements is a(1,1), a(2,1), a(1,2), a(2,2), a(1,3), a(2,3). When performing input/output on arrays, array element order is used.

Array Sections

You can refer to a selected portion of an array as an array. Such a portion is called an array section. An array section has a subscript list that contains at least one subscript that is either a subscript triplet or a vector subscript (see the examples under See Subscript Triplets and See Vector Subscripts below). Note that an array section with only one element is not a scalar.

Subscript Triplets

The three components of a subscript triplet are the lower bound of the array section, the upper bound, and the stride (the increment between successive subscripts in the sequence), respectively. Any or all three can be omitted. If the lower bound is omitted, the declared lower bound of the dimension is assumed. If the upper bound is omitted, the upper bound of the dimension is assumed. If the stride is omitted, a stride of one is assumed. Valid examples of array sections using subscript triplets are:

a(2:8:2) ! a(2), a(4), a(6), a(8)
b(1,3:1:-1) ! b(1,3), b(1,2), b(1,1)
c(:,:,:) ! c

Vector Subscripts

A vector (one-dimensional array) subscript can be used to refer to a section of a whole array. Consider the following example:

integer :: vector(3) = (/3,8,12/)
real :: whole(3,15)
print*, whole(3,vector)

Here the array vector is used as a subscript of whole in the PRINT statement, which prints the values of elements (3,3), (3,8), and (3,12).

Arrays and Substrings

A CHARACTER array section or array element can have a substring specifier following the subscript list. If a whole array or an array section has a substring specifier, then the reference is an array section. For example,

character (len=10), dimension (10,10) :: my_string
my_string(3:8,:) (2:4) = 'abc'

assigns 'abc' to the array section made up of characters 2 through 4 of rows 3 through 8 of the CHARACTER array my_string.