- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Informe de contenido inapropiado
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
- Etiquetas:
- Intel® Fortran Compiler
Enlace copiado
4 Respuestas
- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Informe de contenido inapropiado
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))
- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Informe de contenido inapropiado
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
- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Informe de contenido inapropiado
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.
- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Informe de contenido inapropiado
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
Responder
Opciones de temas
- Suscribirse a un feed RSS
- Marcar tema como nuevo
- Marcar tema como leído
- Flotar este Tema para el usuario actual
- Favorito
- Suscribir
- Página de impresión sencilla