Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 ディスカッション

Character variable with undetermined length

OP1
新規コントリビューター III
940件の閲覧回数

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 件の賞賛
6 返答(返信)
Steven_L_Intel1
従業員
940件の閲覧回数
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.
OP1
新規コントリビューター III
940件の閲覧回数
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
joerg_kuthe
初心者
940件の閲覧回数
Maybe the Fortran module iso_varying_string

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

helps.

Jrg Kuthe
www.qtsoftware.de

Steven_L_Intel1
従業員
940件の閲覧回数
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.
OP1
新規コントリビューター III
940件の閲覧回数
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
Steven_L_Intel1
従業員
940件の閲覧回数
You need 11.1 for this.
返信