Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29251 Discussions

Character variable with undetermined length

OP1
New Contributor III
938 Views

Dear forum,

I have a subroutine which reads a text input file. In this subroutine I need to read (repetitively)a character string S which has a length which is not known prior to the call to this subroutine (in essence, thelengthNis determined by the content of the file).

One way of dealing with this is of course to declare a string S of length much greater than the anticipated possible values of N, and then later on perform operations on S(1:N) only. But this is not very efficient when N can vary in a wide range of values, and when these character operations need to be repeated many, many times. It would be much better to *allocate* S to the right size (once it is known) - but I am not sure that what I am looking for corresponds to allocatable character variables.

Note: once the size of S (i.e., N) is determined by the subroutine, it doesn't change - and then millions of read/processing operations are performed with S.

Is there any convenient way to do this?

Olivier

0 Kudos
6 Replies
Steven_L_Intel1
Employee
938 Views
An allocatable character variable would seem to be a good approach. Do you need to read any of the strings from the file before determining the length? Note that you can't set the size of an allocatable character by reading a string from the file, you need to have allocated it to the correct length first.
0 Kudos
OP1
New Contributor III
938 Views
Thanks Steve,

No, once the size of the string is determined (early on in the file) it does not change.
I looked at allocatable character variables... this is going to make me look real dumb I know... I did the following test case:

PROGRAM MAIN
IMPLICIT NONE
CHARACTER,ALLOCATABLE :: S(:)
ALLOCATE(S(10))
S = '0123456789'
WRITE(*,*) S
WRITE(*,*) S(1:10)
END PROGRAM MAIN

The output is
'0000000000'
'0000000000'

which is not that surprising, of course. But this is not what I want (I want to be able to use S just like a normal character variable, I guess, so that I can use S = 'xxxx' statements, as well as use regular character subroutines (TRIM, ADJUSTL, LEN_TRIM, SCAN etc).

I must be missing something very obvious!

Olivier
0 Kudos
joerg_kuthe
Novice
938 Views
Maybe the Fortran module iso_varying_string

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

helps.

Jrg Kuthe
www.qtsoftware.de

0 Kudos
Steven_L_Intel1
Employee
938 Views
The syntax is a bit different here.

[plain]PROGRAM MAIN
IMPLICIT NONE
CHARACTER(:),ALLOCATABLE :: S
ALLOCATE(CHARACTER(10) :: S)
S = '0123456789'
WRITE(*,*) S
WRITE(*,*) S(1:10)
END PROGRAM MAIN[/plain]
Do not use ISO_VARYING_STRING.
0 Kudos
OP1
New Contributor III
938 Views
Thanks Steve!!

It looks like this is exactly what I want... But I can't get it now... I am going to have to kick and scream to get my hands on IVF 11 (still stuck with IVF 10).
So I suppose I'll have to stick with fixed length string variables.

I made a good note of this however. Thanks for the corrected sample program!

Olivier
0 Kudos
Steven_L_Intel1
Employee
938 Views
You need 11.1 for this.
0 Kudos
Reply