| United States-English |
|
|
|
![]() |
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 4 ArraysArray sections |
|
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 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. 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.
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.
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:
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:
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
Here are the command lines to compile and execute the program, along with the output from a sample run:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||