As mentioned in another example a subset of the elements of an array, called an array section, may be referenced. From that example we may have
real x(10) x(:) = 0. x(2:6) = 1. x(3:4) = [3., 5.]
Array sections may be more general than this, though. They may take the form of subscript triplets or vector subscripts.
A subscript triple takes the form
[bound1]:[bound2][:stride]. For example
real x(10) x(1:10) = ... ! Elements x(1), x(2), ..., x(10) x(1:) = ... ! The omitted second bound is equivalent to the upper, same as above x(:10) = ... ! The omitted first bound is equivalent to the lower, same as above x(1:6:2) = ... ! Elements x(1), x(3), x(5) x(5:1) = ... ! No elements: the lower bound is greater than the upper x(5:1:-1) = ... ! Elements x(5), x(4), x(3), x(2), x(1) x(::3) = ... ! Elements x(1), x(4), x(7), x(10), assuming omitted bounds x(::-3) = ... ! No elements: the bounds are assumed with the first the lower, negative stride
When a stride (which must not be zero) is specified, the sequence of elements begins with the first bound specified. If the stride is positive (resp. negative) the selected elements following a sequence incremented (resp. decremented) by the stride until the last element not larger (resp. smaller) than the second bound is taken. If the stride is omitted it is treated as being one.
If the first bound is larger than the second bound, and the stride is positive, no elements are specified. If the first bound is smaller than the second bound, and the stride is negative, no elements are specified.
It should be noted that
x(10:1:-1) is not the same as
x(1:10:1) even though each element of
x appears in both cases.
A vector subscript is a rank-1 integer array. This designates a sequence of elements corresponding to the values of the array.
real x(10) integer i x([1,6,4]) = ... ! Elements x(1), x(6), x(4) x([(i,i=2,4)]) = ... ! Elements x(2), x(3) and x(4) print*, x([2,5,2]) ! Elements x(2), x(5) and x(2)
An array section with a vector subscript is restricted in how it may be used:
Further, such an array section may not appear in a statement which involves its definition when the same element is selected twice. From above:
print*, x([2,5,2]) ! Elements x(2), x(5) and x(2) are printed x([2,5,2]) = 1. ! Not permitted: x(2) appears twice in this definition
real x(5,2) print*, x(::2,2:1:-1) ! Elements x(1,2), x(3,2), x(5,2), x(1,1), x(3,1), x(5,1)