Further to the question put by Roland Schilling, and Richard Maine's
reply, I agree that the rules of f95 make it easier, but it is possible to
do something in f90, albeit with some difficulty. A discussion (I think
on this list) early last year on how to produce a function which converts
an integer to a trimmed and left-justified string produced a couple of
answers. My contribution was a function of which the declaration part
was:
FUNCTION int2str(intval) RESULT(string)
INTEGER , INTENT(IN) :: intval
INTEGER :: n
CHARACTER(LEN=12 - MAX(0,SIGN(1,intval)) - VERIFY(TRANSFER(ACHAR(48+&
(/(MOD(MAX(1,ABS(intval))/10**n,10),n=9,0,-1)/) ), &
"1234567890"), ACHAR(48)) ) :: string
INTEGER :: absval, j, k
CHARACTER(LEN=11) :: buf
If you inspect the CHARACTER statement carefully you can see how it works.
It seems to be valid Fortran90; at the time it broke several compilers,
though I think they are mostly fixed now.
An alternative formulation, longer but perhaps more elegant was given by
someone else (sorry I lost the author's attribution for the moment):
FUNCTION INT2STR(intval) RESULT(string)
INTEGER , INTENT(IN) :: intval
CHARACTER(LEN=12 - MAX(0,SIGN(1,intval)) - VERIFY( &
ACHAR(48+MOD(ABS(intval)/1000000000,10)) // &
ACHAR(48+MOD(ABS(intval)/100000000,10)) // &
ACHAR(48+MOD(ABS(intval)/10000000,10)) // &
ACHAR(48+MOD(ABS(intval)/1000000,10)) // &
ACHAR(48+MOD(ABS(intval)/100000,10)) // &
ACHAR(48+MOD(ABS(intval)/10000,10)) // &
ACHAR(48+MOD(ABS(intval)/1000,10)) // &
ACHAR(48+MOD(ABS(intval)/100,10)) // &
ACHAR(48+MOD(ABS(intval)/10,10)) // &
ACHAR(48+MOD(MAX(1,ABS(intval)),10)), ACHAR(48))) :: string
INTEGER :: absval, j, k
CHARACTER(LEN=11) :: buf
Perhaps one of these will give Roland Schilling some ideas. :-)
One thing to bear in mind is that Standard Fortran does not permit
recursive I/O, so that if you have a function containing even an internal
file READ or WRITE the function cannot itself be used in the I/O list of
another READ or WRITE statement. This can be a significant restriction.
--
Clive Page,
Dept of Physics & Astronomy,
University of Leicester.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|