Array sections

An array section is a selected portion of an array. It is an array subobject that designates a set of elements from an array, or a specified substring or derived-type component from each of those elements. An array section is also an array.

Note: This introductory section describes the simple case, where structure components are not involved. Array sections and structure components explains the additional rules for specifying array sections that are also structure components.
Read syntax diagramSkip visual syntax diagram
>>---array_name--(--section_subscript_list--)--+-----------------+---><
                                               '-substring_range-'     

section_subscript
designates some set of elements along a particular dimension. It can be composed of a combination of the following:
Read syntax diagramSkip visual syntax diagram
>>-+-subscript---------+---------------------------------------><
   +-subscript_triplet-+   
   '-vector_subscript--'   

subscript
is a scalar integer expression. For details, see Array elements.

IBM extension begins A subscript can be a scalar real expression in XL Fortran. IBM extension ends

subscript_triplet, vector_subscript
designate a sequence of subscripts in a given dimension. For details, see Subscript triplets and Vector subscripts.
Notes:
  • At least one of the dimensions must be a subscript triplet or vector subscript, so that an array section is distinct from an array element. See Example 1.
  • Fortran 2008 beginsAn array section can contain a set of array elements that is contiguous or not contiguous within the array. For more information, see Contiguity.Fortran 2008 ends
substring_range
Read syntax diagramSkip visual syntax diagram
>>-(--+-----------+--:--+-----------+--)-----------------------><
      '-int_expr1-'     '-int_expr2-'      

int_expr1, int_expr2
are scalar integer expressions called substring expressions, defined in Character substrings. They specify the leftmost and rightmost character positions, respectively, of a substring of each element in the array section. If an optional substring_range is present, the section must be from an array of character objects. For details, see Substring ranges.

An array section is formed from the array elements specified by the sequences of values from the individual subscripts, subscript triplets, and vector subscripts, arranged in column-major order. See Example 2.

Examples

Example 1

INTEGER, DIMENSION(5,5,5) :: A
A(1,2,3) = 100
A(1,3,3) = 101
PRINT *, A(1,2,3)    ! A single array element, 100.
PRINT *, A(1,2:2,3)  ! A one-element array section, (/ 100 /)
PRINT *, A(1,2:3,3)  ! A two-element array section,
                     ! (/ 100, 101 /)

Example 2

If SECTION = A(1:3, (/5, 6, 5/), 4)
  • The sequence of numbers for the first dimension is 1, 2, 3.
  • The sequence of numbers for the second dimension is 5, 6, 5.
  • The subscript for the third dimension is the constant 4.
The section is made up of the following elements of A, in this order:
A(1,5,4)   |                        |        SECTION(1,1)
A(2,5,4)   |----- First column -----|        SECTION(2,1)
A(3,5,4)   |                        |        SECTION(3,1)
A(1,6,4)     |                        |      SECTION(1,2)
A(2,6,4)     |----- Second column ----|      SECTION(2,2)
A(3,6,4)     |                        |      SECTION(3,2)
A(1,5,4)       |                        |    SECTION(1,3)
A(2,5,4)       |----- Third column -----|    SECTION(2,3)
A(3,5,4)       |                        |    SECTION(3,3)

Example 3

Some other examples of array sections include:
INTEGER, DIMENSION(20,20) :: A
! These references to array sections require loops or multiple
! statements in FORTRAN 77.
PRINT *, A(1:5,1)                   ! Contiguous sequence of elements
PRINT *, A(1:20:2,10)               ! Noncontiguous sequence of elements
PRINT *, A(:,5)                     ! An entire column
PRINT *, A( (/1,10,5/), (/7,3,1/) ) ! A 3x3 assortment of elements

Related information