Quantcast
Channel: Intel® Software - Intel® Visual Fortran Compiler for Windows*
Viewing all articles
Browse latest Browse all 5691

Recommended method for creating an array of variable length strings

$
0
0

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

 


Viewing all articles
Browse latest Browse all 5691

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>