- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
Dear forum members,
I would be grateful for advice on the "best" practice for creating a dynamic array of Variable Length Strings (VLS) in Fortran. The following is a working skeleton implementation of my objective. Question: is there a better way of doing this? In particular, I would prefer to avoid the need to declare the VLSType. Furthermore, when deallocating the array of VLSType components is it first necessary to deallocate each component in the array?
Many thanks,
David.
PROGRAM TESTVLS ! Skeleton implementation of a list of variable ! length strings in Fortran IMPLICIT NONE ! Define a varaible length string type ! which makes use of Fortran dynamically allocatable ! strings TYPE VLSType CHARACTER (LEN=:), ALLOCATABLE :: S END TYPE INTEGER, PARAMETER :: MX = 10 TYPE(VLSType), DIMENSION(:), ALLOCATABLE :: LIST INTEGER :: I ! Allocate an array of MX variable length strings ALLOCATE(LIST(MX)) ! Populate LIST with MX strings in which the first consists ! of MX 'X' characters the second MX-1 'X' characters and so ! on to the last with a single X character. ! Verify that the length of each string in the list has the ! expected length. DO I = MX, 1, -1 LIST(I)%S = REPEAT('X',I) WRITE(*,FMT = "(I2,2X,'$',A,'$')") LEN(LIST(I)%S), LIST(I)%S ENDDO ! QUESTION: Is it necessary to deallocate each element ! of LIST before deallocating the LIST? DEALLOCATE(LIST) END PROGRAM TESTVLS
- Marcas:
- Intel® Fortran Compiler
Link copiado
- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
You need to create the wrapper type, as you have done, though depending on your circumstances, it may be better off to "borrow" a module that has already been written by others that holds such a wrapper type and supporting procedures.
You do not need to deallocate each element of LIST, nor do you really have to deallocate LIST itself.
- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
It would be nice if Fortran would permit you to declare a type without a member variable. IOW so you could eliminate the %S on LIST(I)%S
If you want, consider using the Fortran PreProcessor, then
#define List(x) LIST(x)%S
Then use the mixed case List in your program.
Jim Dempsey
- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
Dear Jim & Ian,
Thank you for your suggestions.
Ian, are you able to suggest modules that have been written by others and which are in the public domain or under, say, a GPL?
(Through a google search I have found implementations of what I believe to be the now defunct Fortran 90/95 based ISO_Varying_String module.)
Many thanks,
David.
- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
David Vowles wrote:
Dear Jim & Ian,
Thank you for your suggestions.
Ian, are you able to suggest modules that have been written by others and which are in the public domain or under, say, a GPL?
(Through a google search I have found implementations of what I believe to be the now defunct Fortran 90/95 based ISO_Varying_String module.)
Many thanks,
David.
It has been quite a while since I looked at this, and I am just going off my recollection that I completed it, rather than any explicit testing, but here is a variant of Rich Townsend's F95 + allocatable TR implementation of iso_varying_string, modified to use a F2003 deferred length character as the basic storage unit. The chars component of the varying_string type is marked private, but if it was a public component instead - you would have something that looked just like the wrapper that you had in your original post, but with a whole heap of supporting procedures written to give an interface that was, at least at one stage, specified by an ISO standard.
Because that iso_varying_string standard provided a complete specification of an interface, I don't think it is defunct. Interoperability between libraries isn't helped by every library having its own string type - that just makes life harder for client programmers having to map objects from one type to another.
Note I don't always (or perhaps "ever") follow my own advice - I've just found three different modules (and I expect that there are more) that I've written since deferred length character was supported, that provide such a wrapper type and supporting procedures. I've attached one of those, but its interface is nowhere near as rich as that in the aniso_varying_string case.
Personally I avoid the use of the preprocessor.

- Subscrever fonte RSS
- Marcar tópico como novo
- Marcar tópico como lido
- Flutuar este Tópico para o utilizador atual
- Marcador
- Subscrever
- Página amigável para impressora