Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Subscript Triplets

A subscript triplet is a set of three values representing the lower bound of the array section, the upper bound of the array section, and the increment (stride) between them. It takes the following form:

[first-bound] : [last-bound] [:stride]

first-bound

Is a scalar integer (or other numeric) expression representing the first value in the subscript sequence. If omitted, the declared lower bound of the dimension is used.

last-bound

Is a scalar integer (or other numeric) expression representing the last value in the subscript sequence. If omitted, the declared upper bound of the dimension is used.

When indicating sections of an assumed-size array, this subscript must be specified.

stride

Is a scalar integer (or other numeric) expression representing the increment between successive subscripts in the sequence. It must have a nonzero value. If it is omitted, it is assumed to be 1.

The stride has the following effects:

  • If the stride is positive, the subscript range starts with the first subscript and is incremented by the value of the stride, until the largest value less than or equal to the second subscript is attained.

    For example, if an array has been declared as B(6,3,2), the array section specified as B(2:4,1:2,2) is a rank-two array with shape (3,2) and size 6. It consists of the following six elements:

       B(2,1,2)    B(2,2,2)
       B(3,1,2)    B(3,2,2)
       B(4,1,2)    B(4,2,2)  

    If the first subscript is greater than the second subscript, the range is empty.

  • If the stride is negative, the subscript range starts with the value of the first subscript and is decremented by the absolute value of the stride, until the smallest value greater than or equal to the second subscript is attained.

    For example, if an array has been declared as A(15), the array section specified as A(10:3:-2) is a rank-one array with shape (4) and size 4. It consists of the following four elements:

       A(10)
       A(8)
       A(6)
       A(4)  

    If the second subscript is greater than the first subscript, the range is empty.

If a range specified by the stride is empty, the array section has a size of zero.

A subscript in a subscript triplet need not be within the declared bounds for that dimension if all values used to select the array elements are within the declared bounds. For example, if an array has been declared as A(15), the array section specified as A(4:16:10) is valid. The section is a rank-one array with shape (2) and size 2. It consists of elements A(4) and A(14).

If the subscript triplet does not specify bounds or stride, but only a colon (:), the entire declared range for the dimension is used.

If you leave out all subscripts, the section defaults to the entire extent in that dimension. For example:

 REAL A(10)
 A(1:5:2) = 3.0   ! Sets elements A(1), A(3), A(5) to 3.0
 A(:5:2) = 3.0    ! Same as the previous statement
                  !   because the lower bound defaults to 1
 A(2::3) = 3.0    ! Sets elements A(2), A(5), A(8) to 3.0
                  ! The upper bound defaults to 10
 A(7:9) = 3.0     ! Sets elements A(7), A(8), A(9) to 3.0
                  ! The stride defaults to 1
 A(:) = 3.0       ! Same as A = 3.0; sets all elements of
                  !   A to 3.0

See Also