HP Fortran Programmer's Reference (September 2007)

Arrays
Array sections
Chapter 3 71
Example 3-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