Discussion:
Fortran 90 Functions and Character Strings
(too old to reply)
clickety6
2005-07-14 08:16:28 UTC
Permalink
I would like to have a function where I cna pass in an array of
variable length and a charcter string of variable length and have
returned a character string of varibale length. There seems to be lots
of different ways of declaring the type of function (maybe the one
chosen below isn't the best - I'm failry new to F90!), but there seems
to be no way of getting a function to return a string of variable
length. Is this possible? Am I misisng something obvious?

Thanks!

Tim

EXAMPLE:
==========
PROGRAM test

INTEGER(KIND=1):: buffer(10)
CHARACTER(LEN = 10) :: string

string = byte2char(buffer, string)

CONTAINS

FUNCTION byte2char (buffer, string)

INTEGER(KIND=1):: buffer(:)
CHARACTER(LEN = *) :: string
CHARACTER(LEN = *) ::
string = TRANSFER(buffer, string)

END FUNCTION byte2char

END PROGRAM test
James Van Buskirk
2005-07-14 08:37:02 UTC
Permalink
Post by clickety6
I would like to have a function where I cna pass in an array of
variable length and a charcter string of variable length and have
returned a character string of varibale length. There seems to be lots
of different ways of declaring the type of function (maybe the one
chosen below isn't the best - I'm failry new to F90!), but there seems
to be no way of getting a function to return a string of variable
length. Is this possible? Am I misisng something obvious?
INTEGER(KIND=1):: buffer(10)
CHARACTER(LEN = 10) :: string

buffer = 64 ! Initialize buffer

string = byte2char(buffer, string)

write(*,*) string ! write out result

CONTAINS

FUNCTION byte2char (buffer, string)

INTEGER(KIND=1):: buffer(:)
CHARACTER(LEN = *) :: string
! CHARACTER(LEN = *) ::
! string = TRANSFER(buffer, string)
character(LEN = len(string)) :: byte2char

byte2char = TRANSFER(buffer, string)

END FUNCTION byte2char

END PROGRAM test
--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
David Frank
2005-07-14 09:32:31 UTC
Permalink
there seems to be no way of getting a function to return a string of
variable
length
Just about my most desired feature provided in F2003 is allocatable
strings, but there are work-arounds
including external functions without need for nasty interfacing as I just
demo'ed in another topic.

! ---------------------
program test
character(80),external :: b2s
integer(1) :: b1(80) ; equivalence (s1,b1)
character(80) :: s2, s1 = "the quick brown fox jumps over the lazy dog's
back"
s2 = b2s(b1,size(b1))
write (*,*) s2(1:len_trim(s1)) // '.' ! end sentence with a .
end program
! ---------------------------
function b2s(b,n) result (s)
integer :: n
integer(1) :: b(n)
character(n) :: s
s = transfer(b,s)
end function b2s
David Flower
2005-07-14 12:42:49 UTC
Permalink
Am I right in thinking that you come from a C background, and are
relatively new to any version of FORTRAN ?

An essential feature of FORTRAN is that variables of type CHARACTER are
of fixed length. This is true even if the CHARACTER variable is
ALLOCATEd.
David Frank
2005-07-14 13:51:47 UTC
Permalink
Post by David Flower
Am I right in thinking that you come from a C background, and are
relatively new to any version of FORTRAN ?
An essential feature of FORTRAN is that variables of type CHARACTER are
of fixed length. This is true even if the CHARACTER variable is
ALLOCATEd.
One can write a function that returns a VARYING length string and in fact I
just did in my earlier than your
reply.

! ---------------------------
function b2s(b,n) result (s)
integer :: n
integer(1) :: b(n)
character(n) :: s
s = transfer(b,s)
end function b2s

And in F2003 we have the syntax available to complete this operation..

character(:),allocatable :: string
string = b2s(b,n)

subsequent string assignments will also change the length of string
string = 'hello world'

Your statement therefore is incorrent, the Fortran standard supports varying
strings..

Continue reading on narkive:
Loading...