- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
I have written a small function called STRIM, very like TRIM, except that it also strips off traling null characters. In the function itself I have declared it as:
character*(*) function strim(string)
But how do I declare it in the calling routine? It works if I declare it as:
character*N strim
where N can be any integer, but of course I don't usually know what N is.
Is this the wrong approach? Any suggestions?
Many thanks
Mike
character*(*) function strim(string)
But how do I declare it in the calling routine? It works if I declare it as:
character*N strim
where N can be any integer, but of course I don't usually know what N is.
Is this the wrong approach? Any suggestions?
Many thanks
Mike
- Balises:
- Intel® Fortran Compiler
Lien copié
4 Réponses
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
Why not just return the length of the new string?
E.g
print *, string(1:len_strim(string))
E.g
print *, string(1:len_strim(string))
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
TRIM is rather unusual - there is no good way to "write your own" function that does what it does. Alfred's suggestion is a good one.
Steve
Steve
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
As the original poster discovered yourself, CHARACTER(*) functions do not do what one might think -- actually, they're a quirk in F9x and considered obsolete.
However, there is a way to do the similar as TRIM: in Fortran-95, length-specification-statements can be result of user-written PURE functions. Of course, entire thing needs an explicit interface, so it's best to place entire thing in a module:
P.S. Actually, this is a field which caused many compilers to cough, especially when sizing expression is complex. This case is rather simple; CVF is hopefully mature enough to handle even more complex cases.
However, there is a way to do the similar as TRIM: in Fortran-95, length-specification-statements can be result of user-written PURE functions. Of course, entire thing needs an explicit interface, so it's best to place entire thing in a module:
MODULE Strings !=========== CONTAINS !=========== FUNCTION CTRIM(sStr) CHARACTER*(*),INTENT(IN):: sStr CHARACTER(LEN=CLEN(sStr)):: CTRIM CTRIM = sStr END FUNCTION CTRIM !=========== PURE INTEGER FUNCTION CLEN(szString) CHARACTER(*),INTENT(IN):: szString CLEN = INDEX(szString,CHAR(0)) - 1 IF (CLEN==0) CLEN = LEN_TRIM(szString) END FUNCTION CLEN !=========== END MODULE StringsJugoslav
P.S. Actually, this is a field which caused many compilers to cough, especially when sizing expression is complex. This case is rather simple; CVF is hopefully mature enough to handle even more complex cases.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
Even sleazier:
Function fullTrim(Input)
implicit none
Character(*),intent(in):: Input
integer(1) i
Character(Len_trim(Input)-verify(Input,' ')+1):: FullTrim
i=verify(Input,' ')
fullTrim=Input(i:)
end Function fullTrim
Function fullTrim(Input)
implicit none
Character(*),intent(in):: Input
integer(1) i
Character(Len_trim(Input)-verify(Input,' ')+1):: FullTrim
i=verify(Input,' ')
fullTrim=Input(i:)
end Function fullTrim
Répondre
Options du sujet
- S'abonner au fil RSS
- Marquer le sujet comme nouveau
- Marquer le sujet comme lu
- Placer ce Sujet en tête de liste pour l'utilisateur actuel
- Marquer
- S'abonner
- Page imprimable