Subscript triplets

A subscript triplet consists of two subscripts and a stride, and defines a sequence of numbers corresponding to array element positions along a single dimension.

Read syntax diagramSkip visual syntax diagram
>>-+------------+--:--+------------+--+-----------+------------><
   '-subscript1-'     '-subscript2-'  '-:--stride-'   

subscript1
is the subscript that designates the first value in the sequence of indices for a dimension.

If it is omitted, the lower array bound of that dimension is used.

subscript2
is the subscript that designates the last value in the sequence of indices for a dimension.

If it is omitted, the upper array bound of that dimension is used. It is mandatory for the last dimension when specifying sections of an assumed-size array.

stride
is a scalar integer expression that specifies how many subscript positions to count to reach the next selected element.

A stride can be a scalar real expression in XL Fortran.

If the stride is omitted, it has a value of 1. The stride must have a nonzero value:
  • A positive stride specifies a sequence of integers that begins with the first subscript and proceeds in increments of the stride to the largest integer that is not greater than the second subscript. If the first subscript is greater than the second, the sequence is empty.
  • When the stride is negative, the sequence begins at the first subscript and continues in increments specified by the stride to the smallest integer equal to or greater than the second subscript. If the second subscript is greater than the first, the sequence is empty.

Calculations of values in the sequence use the same steps as shown in Executing a DO statement.

A subscript in a subscript triplet does not have to be within the declared bounds for that dimension if all the values used in selecting the array elements for the array section are within the declared bounds:
INTEGER A(9)
PRINT *, A(1:9:2)  ! Count from 1 to 9 by 2s: 1, 3, 5, 7, 9.
PRINT *, A(1:10:2) ! Count from 1 to 10 by 2s: 1, 3, 5, 7, 9.
                   ! No element past A(9) is specified.

Examples

REAL, DIMENSION(10) :: A
INTEGER, DIMENSION(10,10) :: B
CHARACTER(10) STRING(1:100)

PRINT *, A(:)                 ! Print all elements of array.
PRINT *, A(:5)                ! Print elements 1 through 5.
PRINT *, A(3:)                ! Print elements 3 through 10.

PRINT *, STRING(50:100)       ! Print all characters in
                              ! elements 50 through 100.

! The following statement is equivalent to A(2:10:2) = A(1:9:2)
A(2::2) = A(:9:2)             ! LHS = A(2), A(4), A(6), A(8), A(10)
                              ! RHS = A(1), A(3), A(5), A(7), A(9)
                              ! The statement assigns the odd-numbered
                              ! elements to the even-numbered elements.

! The following statement is equivalent to PRINT *, B(1:4:3,1:7:6)
PRINT *, B(:4:3,:7:6)         ! Print B(1,1), B(4,1), B(1,7), B(4,7)

PRINT *, A(10:1:-1)           ! Print elements in reverse order.

PRINT *, A(10:1:1)            ! These two are
PRINT *, A(1:10:-1)           ! both zero-sized.
END