Silverfrost Forums

Welcome to our forums

\"CHARACTER (LEN = ALLOCATABLE)\"

26 Aug 2010 1:44 #6839

I've established that this is not possible in any flavour of FORTRAN before 2003, and often not then. On the other hand, there are various more or less acceptable workarounds e.g. a fixed-length string longer than necessary, combined with TRIM. This is only OK if one can identify such a length that is not impractically large.

I did find, here:

http://www.star.le.ac.uk/~cgp/f90course/f90.html#tth_sEc7.1

the following tantalizing claim:

*Character functions may return a string with a length which depends on the function arguments, e.g. *

   FUNCTION concat(s1, s2)
   IMPLICIT NONE
   CHARACTER(LEN=LEN_TRIM(s1)+LEN_TRIM(s2)) :: concat  ! function name
   CHARACTER(LEN=*), INTENT(IN) :: s1, s2
   concat = TRIM(s1) // TRIM(s2)
   END FUNCTION concat

I have two questions about this:

  1. It does seem to rely on the trick of 'deferring' declaration of the type of the function until computation has begun. I know this is legitimate, but more commonly in my experience it's a matter of taste whether one writes (in pseudo-code) FUNCTION F followed by TYPE F, or simply TYPE FUNCTION F. Is there a legitimate way to render this function with an up-front type declaration? It seems not, and that troubles me :?

  2. I don't see how one would usefully use this function in practice. The function result still has to assigned to a variable of type CHARACTER, say s3, which still has to be of fixed length. One could equally well go more briefly:

s3 (1: len_trim (s1)) = trim (s1) s3 (len_trim (s1) + 1: len_trim (s1) + len_trim (s2)) = trim (s2)

(admittedly, it would be shorter to build the function if there was a lot of this stuff going on, and it would also avoid the possibility of programmer errors in the indexing). So is this function anything more than a proof of concept, as it were?

26 Aug 2010 4:53 #6842

If all that is wanted is removal of trailing blanks and concatenation, one can use the built-in function trim in a character expression, instead of managing subscripts or calling other functions. For example:

character(len=5) :: a,b,c
a='ti   '; b='ght  ';
c=trim(a)//trim(b)
Please login to reply.