Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 4 Arrays

Array sections

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

An array section is a selected portion of another array (the parent) that is itself an array, even if it consists of only one element, or possibly none. An array section can appear wherever an array name is allowed.

The syntax for specifying an array section is:

array-name (section-subscript-list)[
(substring-range)
]

where:

section-subscript-list

is a comma-separated list of section-subscript.

section-subscript

is one of:

  • subscript

  • subscript-triplet

  • vector-subscript

subscript

is a scalar integer expression.

subscript-triplet

takes the form:

[subscript]:[subscript][:stride]

where stride is a scalar integer expression.

vector-subscript

is a rank-one integer array expression.

substring-range

specifies a character substring, as described in “Character substrings”. If substring-range is specified, array-name must be of type character.

Section-subscript-list must specify section-subscript for each dimension of the parent array. The rank of the array section is the number of subscript-triplets and vector -subscripts that appear in the section-subscript-list. Because an array section is also an array, at least one subscript-triplet or vector-subscript must be specified.

The following sections provide more information about subscript-triplet and vector-subscript.

Subscript triplet

A subscript triplet selects elements from the parent array to form another array. It specifies a lower bound, an upper bound, and a stride for any dimension of the parent array. Elements are selected in a regular manner from a dimension. The stride can, for example, select every second element.

All three components of a subscript triplet are optional. If a bound is omitted, it is taken from the parent array. However, an upper bound must be specified if a subscript triplet is used in the last dimension of an assumed-sized array.

A bound in a subscript triplet need not be within the declared bounds for that dimension of the parent array if all the elements selected are within its declared bounds. If the stride is omitted, the default is to increment by one.

The stride must not be zero. If it is positive, the subscripts range from the lower bound up to and including the upper bound, in steps of stride. When the difference between the upper bound and lower bound is not a multiple of the stride, the last subscript value selected by the subscript triplet is the largest integer value that is not greater than the upper bound. The array expression a(1: 9: 3) selects subscripts 1, 4, and 7 from a.

Strides may also be negative. A negative stride selects elements from the parent array starting at the lower bound and proceeds backwards through the parent array in steps of the stride down the last value that is greater than the upper bound. For example, the expression a(9:1:- 3) selects the subscripts 9, 6, and 3 in that order from a.

If the section bounds are such that no elements are selected in a dimension (for example, the section a(2:1)), the section has zero-size.

The following example shows subscript triplet notation assigning the same value to a regular pattern of array elements.

INTEGER, DIMENSION(3,6) :: x,y,z   ! declare 3 3x6 arrays

! initialize the arrays, using whole-array assignments.
x = 0; y = 0; z = 0

! assign to elements of x, y, and z, using subscript triplets
x(3,2:4:1) = 1
y(2,2:6:2) = 2
z(1:2,3:6) = 3

! The arrays x, y, and z now have the following values:
! x y z
! 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3
! 0 0 0 0 0 0 0 2 0 2 0 2 0 0 3 3 3 3
! 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

In the following example of an array substring, the variable dates(5:10) is an array section that includes elements 5 through to 10 of the parent array dates, and the variable dates(5:10)(8:11) is also an array section of the array dates but only contains the last 4 character positions of the elements 5 through to 10.

CHARACTER(11) :: dates(20)
dates(5:10)(8:11) = "1776"

Vector subscripts

A vector subscript is any expression that results in a rank-one array with integer value. The values of the array select the corresponding elements of the parent array for a given dimension. Vector subscripts can describe an irregular pattern and may be useful for indirect array addressing. For example, if v represents a rank-one integer array initialized with the values 4, 3, 1, 7, then the array section a(v) is a rank-one array composed of the array elements a(4), a(3), a(1), and a(7)—in that order.

Vector subscripts are commonly specified using array constructors, which are described in the next section. For example, the expressions a(v) and a((/ 4, 3, 1, 7/)) reference the same section of the array a.

Vector subscripts may not appear:

  • On the right hand side of a pointer assignment statement.

  • In an I/O statement as an internal file.

  • As an actual argument that is associated with a dummy argument declared with INTENT(OUT) or INTENT(INOUT) or with no INTENT.

A vector subscript may specify the same element more than once. When a vector subscript of this form specifies an array section, the array section is known as a many-one array section. An example of a many-one array section is:

a( (/ 4, 3, 4, 7/) )

where element 4 has been selected twice. A many-one array section may not appear in either an input list or on the left-hand side of an assignment statement.

The following example, vector_sub.f90, illustrates an array section using a section subscript list.

Example 4-3 vector_sub.f90

PROGRAM main
! m is a rank-1 array that has been
! initialized with the values of an array constructor INTEGER, DIMENSION(4) :: m = (/ 2, 3, 8, 1/)

INTEGER :: i

! initialize a (a rank-1 array) with the values
! 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 11.0
REAL, DIMENSION(10) :: a = (/ (i*1.1, i=1,10) /)

! b is an uninitialized 4x2 array
REAL, DIMENSION(4,2) :: b

! print a section of a, using a vector subscipt
PRINT *,a(m)

! assign the values 5.5, 11.0, 6.6, and 5.5 to the first column
! b; this is an example of a many-one array
b(:,1) = a( (/ 5, 10, 6, 5/) )

! the vector subscript MIN(m,4) represents a rank-1 array with
! the values 2, 3, 4, 1; the second column of b is assigned
! the values 11.0, 6.6, 5.5, 5.5
b(:,2) = b(MIN(m,4),1)

! increment a(2), a(3), a(8), and a(1) by 20.0
a(m) = a(m) + 20.0

! print the new values in a
PRINT *,a
END PROGRAM main

Here are the command lines to compile and execute the program, along with the output from a sample run:

$ f90 vector_sub.f90
$ a.out
2.2 3.3 8.8 1.1
21.1 22.2 23.3 4.4 5.5 6.6 7.7 28.8 9.9 11.0
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.