 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
sparge
Joined: 11 Apr 2005 Posts: 371
|
Posted: Thu Aug 26, 2010 2:44 pm Post subject: "CHARACTER (LEN = ALLOCATABLE)" |
|
|
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.
Code: |
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? |
|
Back to top |
|
 |
mecej4
Joined: 31 Oct 2006 Posts: 1899
|
Posted: Thu Aug 26, 2010 5:53 pm Post subject: trimming and concatenating strings in Fortran 95 |
|
|
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:
Code: |
character(len=5) :: a,b,c
a='ti '; b='ght ';
c=trim(a)//trim(b)
|
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|