HP Fortran Programmer's Reference (September 2007)

Arrays
Array-valued structure component references
Chapter 3 73
The next statement uses a subscript triplet in an array-valued structure component reference
to assign the same score to one test of three students in one course:
course(2)%student(1:3)%test(4) = 82
It would be convenient if we could initialize all tests of all students in all courses to 0. But the
Standard does not allow structure component references in which more than one of the parts
speciļ¬es a rank greater than 0. In other words, the following is not legal:
course%student%test = 0 ! ILLEGAL
The following example, array_val_ref.f90, contains the code examples listed in this section:
Example 3-4 array_val_ref.f90
PROGRAM main
! illustrates array-valued structure component references
! define a derived type that will be used to declare an
! object of this type as a component of another derived type
TYPE student_data
CHARACTER(25) :: name
INTEGER :: average, test(4)
END TYPE student_data
TYPE course_data
CHARACTER(25) :: course_title
INTEGER :: course_num, class_size
TYPE(student_data) :: student(10) ! an array of derived
! type
END TYPE course_data
TYPE (course_data) :: course(5) ! an array of derived
! type
! scalar-valued structure component reference
course(5)%student(7)%test(4) = 95
PRINT *, course(5)%student(7)%test(4)
! array-valued structure component reference
course(4)%student%test(3) = 60
PRINT *, course(4)%student%test(3)
! array-valued structure component reference
course(3)%student(3)%test = 0
PRINT *, course(3)%student(3)%test
! array-valued structure component reference, using
! a subscript triplet to reference a section of the
! array component student
course(2)%student(1:3)%test(4) = 82