Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

allocate strings in fortran 90?

598 views
Skip to first unread message

Ed

unread,
Sep 1, 2009, 3:55:09 PM9/1/09
to
Howdy all!

Given the length of a string in a file, how do I allocate a F90
character variable to hold it?

Thanks,

Ed

Richard Maine

unread,
Sep 1, 2009, 4:15:49 PM9/1/09
to
Ed <edwardjam...@gmail.com> wrote:

> Given the length of a string in a file, how do I allocate a F90
> character variable to hold it?

Allocatable string length is an f2003 feature (and one awfully slow to
get implemented in actual compilers). You can't do it in f90 (or f95),
at least not simply.

I think I might have seen a horribly ugly hack that involved allocating
a character array and using argument sequence association to map a
character string onto the allocated memory. It might have been by James
Buskirk, but I wouldn't swear to that. (It being a TRANSFER-like trick,
he comes to mind). It isn't something I'd do.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain

James Van Buskirk

unread,
Sep 1, 2009, 9:17:46 PM9/1/09
to
"Richard Maine" <nos...@see.signature> wrote in message
news:1j5d8ns.1gwqs5i1afydt4N%nos...@see.signature...

> horribly ugly hack
> James
> Buskirk TRANSFER

;)

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


Richard Maine

unread,
Sep 1, 2009, 9:38:07 PM9/1/09
to
James Van Buskirk <not_...@comcast.net> wrote:

> "Richard Maine" <nos...@see.signature> wrote in message
> news:1j5d8ns.1gwqs5i1afydt4N%nos...@see.signature...
>
> > horribly ugly hack
> > James
> > Buskirk TRANSFER
>
> ;)

Oh dear! I tried real hard to both say what I thought about it, but not
sound insulting. It occurred to me that there was a potential danger of
that. :-(

James Van Buskirk

unread,
Sep 1, 2009, 10:43:52 PM9/1/09
to
"Richard Maine" <nos...@see.signature> wrote in message
news:1j5dnn8.giqmk2yjfoc0N%nos...@see.signature...

> Oh dear! I tried real hard to both say what I thought about it, but not
> sound insulting. It occurred to me that there was a potential danger of
> that. :-(

I wasn't insulted, just amused at being equated with Demon Rum in
your sermon. :)

Dave Allured

unread,
Sep 2, 2009, 4:18:34 AM9/2/09
to

You mean using fortran 90 rules, right? Under F90 you can't do exactly
what you are thinking. I wish you could. However there are several
useful approximations. Following from memory, untested...

Method 1. Over allocate and trim.

character line*200 ! longer than longest expected string
read (infile, '(a)') line
print *, '[' // trim (line) // ']'

Method 2. Over allocate and substring.

character line*200 ! longer than longest expected string
integer len1
read (infile, '(a)') line
len1 = len_trim (line) ! get len by any method desired
print *, '[' // line(1:len1) // ']'

Method 3. Character*1 allocated array. Cumbersome...

character*1, allocatable :: chars(:)
integer len1
len1 = ... predetermined, you said "given the length'...
allocate (chars(len1))
read (infile, '(9999a1)') chars ! 9999 longer than max possible
! must be expected length on one line within file
print *, '[', chars, ']' ! unsure, may require further handling

Method 4. Use subroutine interface to make conformance.
Within subroutine only, string will be expected length.

integer len1
len1 = ... predetermined, you said "given the length'...
call read_string (len1)

subroutine read_string (len2)
integer len2
character(len2) str
read (infile, '(a)') str
print *, '[' // str // ']'
end subroutine read_string

I use a mixture of 1 and 2 all the time to get real work done. I do not
recommend 3 at all, 4 only when really necessary. HTH.

--Dave

SimonG

unread,
Sep 2, 2009, 4:37:18 AM9/2/09
to

As others have already said you can't do this in F90. You could try
the ISO Varying String module:

http://www.fortran.com/iso_varying_string.f95

which may be suitable for your purpose.

Regards,

Simon


Arjen Markus

unread,
Sep 2, 2009, 4:52:26 AM9/2/09
to

An alternative is to use the following:

length = determine_string_length( filename )
call handle_file( filename, length)

subroutine handl_file( filename, length )
character(len=*) :: filename
integer :: length
character(len=length) :: buffer

... use the local variable buffer to read the file
... line by line

end subroutine


Regards,

Arjen

robin

unread,
Sep 2, 2009, 6:40:27 AM9/2/09
to
"Richard Maine" <nos...@see.signature> wrote in message
news:1j5d8ns.1gwqs5i1afydt4N%nos...@see.signature...

> Ed <edwardjam...@gmail.com> wrote:
>
>> Given the length of a string in a file, how do I allocate a F90
>> character variable to hold it?
>
> Allocatable string length is an f2003 feature (and one awfully slow to
> get implemented in actual compilers).

It's something that's been available in PL/I since 1966.


robin

unread,
Sep 2, 2009, 6:40:27 AM9/2/09
to
"Ed" <edwardjam...@gmail.com> wrote in message
news:0efb23e2-d9bc-4eb1...@i8g2000pro.googlegroups.com...

> Howdy all!
>
> Given the length of a string in a file, how do I allocate a F90
> character variable to hold it?

Read N and then call SUB:

SUBROUTINE SUB(N)
IMPLICIT NONE
INTEGER :: N
CHARACTER (LEN=N) :: S
READ *, S
PRINT *, S
END SUBROUTINE SUB


0 new messages