Re: Reference to derived type element by index?
- From: jwm <jwmwalrus@xxxxxxxxx>
- Date: Sun, 30 Mar 2008 00:33:25 -0700 (PDT)
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
!------------------------------------------------------------------------------
.
- References:
- Reference to derived type element by index?
- From: Rob Crain
- Reference to derived type element by index?
- Prev by Date: Re: LAPACK string orchestra
- Next by Date: data with metadata
- Previous by thread: Re: Reference to derived type element by index?
- Next by thread: Re: Reference to derived type element by index?
- Index(es):
Relevant Pages
|
|