|
Prev: How to Read csv Files with both Characters and Numbers?
Next: what is wrong with this code?can someone walk me through this?
From: Rob Crain on 29 Mar 2008 20:11 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!
From: Gary Scott on 29 Mar 2008 20:44 Rob Crain 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! type circle_datatype real :: radius(100) real :: x(100) real :: y(100) real :: z(100) character(len=256) :: circle_label(100) endtype circle_datatype type(circle_datatype) :: circle circle%radius(1) = <something> -- Gary Scott mailto:garylscott(a)sbcglobal dot net Fortran Library: http://www.fortranlib.com Support the Original G95 Project: http://www.g95.org -OR- Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html If you want to do the impossible, don't hire an expert because he knows it can't be done. -- Henry Ford
From: Gary Scott on 29 Mar 2008 20:50 Gary Scott wrote: > Rob Crain 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! > > type circle_datatype > real :: radius(100) > real :: x(100) > real :: y(100) > real :: z(100) > character(len=256) :: circle_label(100) > endtype circle_datatype > type(circle_datatype) :: circle > > circle%radius(1) = <something> > > Oops or using your original type declaration, another possibility: type(circle_datatype) :: circle(100) circle(1)%radius = something -- Gary Scott mailto:garylscott(a)sbcglobal dot net Fortran Library: http://www.fortranlib.com Support the Original G95 Project: http://www.g95.org -OR- Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html If you want to do the impossible, don't hire an expert because he knows it can't be done. -- Henry Ford
From: Rob Crain on 29 Mar 2008 20:59 Hi Gary - thanks for your reply. I think my initial explanation may have been unclear, as this is not the solution to my problem. The problem reduces to the following: I don't know explicitly which variable (I incorrectly referred to this variable as an element previously, hence the confusion) I want to update, so I can't say circle%radius = <something> All I have is an index that tells me I need to update the i^th variable within the derived type. So say I need to update circle%z, the index would have value 4. But I know that I can't do circle%(4) = <something> but hoped there was some syntax that might allow this procedure? Any help appreciated --Rob Gary Scott wrote: > Rob Crain 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! > type circle_datatype > real :: radius(100) > real :: x(100) > real :: y(100) > real :: z(100) > character(len=256) :: circle_label(100) > endtype circle_datatype > type(circle_datatype) :: circle > > circle%radius(1) = <something> > >
From: Gary Scott on 29 Mar 2008 21:03
Rob Crain wrote: > Hi Gary - thanks for your reply. I think my initial explanation may have > been unclear, as this is not the solution to my problem. The problem > reduces to the following: I don't know explicitly which variable (I > incorrectly referred to this variable as an element previously, hence > the confusion) I want to update, so I can't say > > circle%radius = <something> > > All I have is an index that tells me I need to update the i^th variable > within the derived type. So say I need to update circle%z, the index > would have value 4. But I know that I can't do By i'th variable, are you saying the i'th component? No you can't alias an index with the formally defined components and have value 1 refer to radius and value 2 refer to x. You could use select case to make that choice based upon the index. Still not sure I understand fully tho. > <snip> -- Gary Scott mailto:garylscott(a)sbcglobal dot net Fortran Library: http://www.fortranlib.com Support the Original G95 Project: http://www.g95.org -OR- Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html If you want to do the impossible, don't hire an expert because he knows it can't be done. -- Henry Ford |