M.E.
M.E.

Reputation: 5515

Maxval for derived type's variable in array

I have the following array of derived type in Fortran 90:

type tpo_line
    integer             :: price    = -1
    integer             :: count    = -1
end type

type(tpo_line),dimension(10000) :: myarray

Given a populated myarray, is there a more straightforward way to get the tpo_line item of the array which has the largest count than iterating over the whole array? (I have seen that for simple arrays you can use maxval)

Upvotes: 2

Views: 255

Answers (1)

francescalus
francescalus

Reputation: 32451

A data reference can be an array even when the part on the right is a scalar. As you can have MAXVAL(named_array) you can have MAXVAL(array_ref):

print*, MAXVAL(myarray(:)%count)  ! (:) added for clarity but not needed

Because the component count is scalar we are allowed to have the single part to the left of it an array, and then the reference itself is a (rank 1) array, on which MAXVAL happily works.

Similarly, MAXLOC and other array querying functions will consider myarray%count as a suitable array.

This does not apply when the count component is itself an array.

Upvotes: 2

Related Questions