Re: variable length of integer



relaxmike wrote:
What we really need is the following :

type ( varying_string ) :: mystring
mystring = stringformat ( 2008 )
call stringprint ( mystring )

but we do not have this in standard fortran.

Any other idea ?

Regards,

Michaël Baudin


You can use the techniques you describe to write a "stringformat" or "IntToString" function in Fortran. That can then be used in concatenations, I/O statements or whatever, so that you can get away without the varying_string type.

Here's one I use - it has 2 different ways to compute the string length. RealToString is altogether more complicated.

program testIntToString
print *,('"',inttostring(i),'",',i=-100,100)
contains

function IntToString(i) ! This version can be used in I/O statements
implicit none
integer :: i
! The following elaborate declaration is required to define a string
! having just enough positions to hold all the digits of the integer,
! and the sign character.
! It contains constants no greater than 10000, so as to be acceptable
! on machines having a default integer kind of 16 bits or more.
! (borrowed from "Algorithms and Data Structures in F and Fortran"
! By Robin A. Vowels - Published by Unicomp, 1998. ISBN 0-9640135-4-1)
! character (len = 1 + & ! For 1-digit
! min (abs(i/10), 1) + & ! For 2-digit->1
! min (abs(i/100), 1) + & ! For 3-digit->1
! min (abs(i/1000), 1) + & ! For 4-digit->1
! min (abs(i/10000), 1) + & ! For 5-digit->1
! min (abs(i/10000)/10, 1) + & ! For 6-digit->1
! min (abs(i/10000)/100, 1) + & ! For 7-digit->1
! min (abs(i/10000)/1000, 1) + & ! For 8-digit->1
! min (abs(i/10000)/10000, 1) + & ! For 9-digit->1
! min (abs(i/10000)/10000/10, 1) + & ! For 10 digits
! min (abs(i/10000)/10000/100, 1) + & ! For 11 digits
! min (abs(i/10000)/10000/1000, 1) + & ! For 12 digits
! min (abs(i/10000)/10000/10000, 1) + & ! For 13 digits
! min (abs(i/10000)/10000/10000/10, 1) + & ! For 14 digits
! min (abs(i/10000)/10000/10000/100, 1) + & ! For 15 digits
! min (abs(i/10000)/10000/10000/1000, 1) + & ! For 16 digits
! min (abs(i/10000)/10000/10000/10000, 1) + & ! For 17 digits
! min (abs(i/10000)/10000/10000/10000/10, 1) + & ! For 18 digits
! min (abs(i/10000)/10000/10000/10000/100,1) - & ! For 19 digits
! min (max(i, -1), 0) & ! For -ive nums
! ) :: IntToString
character( max(1, int(log10(real(abs(i))+0.1)) + (3-sign(1,i))/2) )&
:: IntToString
integer :: val , p , izero=iachar('0')

if ( i<0 ) then
IntToString(1:1) = '-'
elseif ( i==0 ) then
IntToString(1:1) = '0'
endif
val = abs(i)

p = len(IntToString)
do while( val>0 )
IntToString(p:p) = achar(izero + mod(val,10))
val = val/10
p = p - 1
end do
end function IntToString
end program testIntToString

--
John Appleyard - (send email to john!news@.. rather than spamtrap@..)
Polyhedron Software
Programs for Programmers - QA, Compilers, Graphics, Consultancy
********* Visit our Web site on http://www.polyhedron.co.uk/ *********
.



Relevant Pages

  • Re: Variable length/precision formats?
    ... "&" is contunuation in Fortran anyway. ... purpose of string substitution. ... Something more general purpose, although for general purpose use, I'd prefer it also support substitution of character strings (, not that it makes any sense for the numerical portion of a format, but you could use it to change from say I format to Z format (and in a wide variety of other places as well). ...
    (comp.lang.fortran)
  • Re: ATTRIBUTES problem
    ... personally use Compaq Fortran and Microsoft C++. ... The string length is a big issue. ... and the Fortran code may well not do what you ... > fortran compiler for windows I'm trying to get acquainted with how strings ...
    (comp.lang.fortran)
  • Re: Launching Another Executable from my Program (Borland C++)
    ... command line args) from a DOS C++ app? ... supplied with MS Fortran F77 and C compilers. ... c The systemfunction passes the given C string (00hex ... c form of spawn is executed as follows: ...
    (comp.os.msdos.programmer)
  • Re: Fortran-compiled DLLs in Python
    ... WindowsError: exception: access violation ... > I do not know how fortran passes this parameter, ... strings may pass a "string descriptor", ...
    (comp.lang.python)
  • Re: ATTRIBUTES problem
    ... >!dec$ attributes reference:: mystring ... but there are A LOT of issues with mixing Fortran and> C/C++ ... > The string length is a big issue. ... > subroutine test ...
    (comp.lang.fortran)