Re: Reference to derived type element by index?



On Mar 29, 6:11 pm, Rob Crain <r.a.cr...@xxxxxxxxxxxx> wrote:
I have a derived type, e.g.

type circle_datatype
    real             :: radius
    real             :: x
    real             :: y
    real             :: z
    character(len=*) :: circle_label
endtype circle_datatype
type(circle_datatype) :: circle

and want to refer to the individual elements via some indexing scheme,
say in this example I want to change the value of circle%radius I would
use index #1, or the z-coordinate I would use #4. Is there some method
by which this is possible?

I think IDL has a system for this, such that circle.radius can be
referenced by circle.(0), but I really need Fortran's horsepower for
this code!

The following might help:

!------------------------------------------------------------------------------
module circle_mod

implicit none

integer, parameter :: MAX_CIRCLE_LABEL = 64

type circle_datatype
real, pointer :: radius => null()
real, pointer :: x => null()
real, pointer :: y => null()
real, pointer :: z => null()
real :: set(4) = 0.
character(MAX_CIRCLE_LABEL) :: label
end type circle_datatype

contains
subroutine create_circle(this, label)
type(circle_datatype), target, intent(INOUT) :: this
character(*), intent(IN) :: label
this%radius => this%set(1)
this%x => this%set(2)
this%y => this%set(3)
this%z => this%set(4)
this%label = ADJUSTL(label)
end subroutine

subroutine destroy_circle(this)
type(circle_datatype), intent(INOUT) :: this
nullify(this%radius, this%x, this%y, this%z)
this%set = 0.
this%label = ''
end subroutine

end module circle_mod

program circle_test

use circle_mod

implicit none

type(circle_datatype), target :: circle

call create_circle(circle, 'unit_xy1')

circle%set = REAL([1, 1, 1, 0])
write (*, '((A,G13.6))') "circle's label = ", circle%label, &
"radius = ", circle%radius, 'x = ', circle%x, 'y = ', &
circle%y, 'z = ', circle%z

call destroy_circle(circle)

stop

end program circle_test
!------------------------------------------------------------------------------
.



Relevant Pages

  • Re: Reference to derived type element by index?
    ... subroutine create_circle(this, label) ... type, target, intent:: this ... type(circle_datatype), target:: circle ...
    (comp.lang.fortran)
  • Re: Reference to derived type element by index?
    ... subroutine create_circle(this, label) ... type, target, intent:: this ... type(circle_datatype), target:: circle ...
    (comp.lang.fortran)
  • TARGET components?
    ... corresponding to TARGET dummy, and do the assignment there, but is it ... I know that when a TARGET actual argument is associated with TARGET ... subroutine set_preferred ... pointers, I face problems like this constantly. ...
    (comp.lang.fortran)
  • Re: Form instance using variable for name
    ... Here is the entire subroutine I have so far. ... Dim ctl As Control ... Dim lbl As Label ...
    (microsoft.public.access.forms)
  • Re: long running perl programs & memory untilization
    ... >below that it still leaks if you use systemto run v4lctl, ... Good catch, I replaced the addtiem subroutine with a simple rename call, ... # Argument 2 is the label to apply ... "They that would give up essential liberty for temporary safety deserve ...
    (comp.lang.perl.misc)