在Fortran语言字符串中提取整数 [英] Extract integers from string in Fortran
问题描述
我有一个字符串声明为字符(20)::折叠
,这是从一个命令行参数,看起来像分配它的价值 X:Y:Z
其中x,y和z都是整数。然后,我需要挑选出的号码的开出该字符串并将其分配给相应的变量。这就是我试图做的:
I'm using Fortran 90. I have a string declared as CHARACTER(20) :: Folds
, which is assigned its value from a command line argument that looks like x:y:z
where x, y, and z are all integers. I then need to pick out the numbers out of that string and to assign them to appropriate variables. This is how I tried doing it:
i=1
do j=1, LEN_TRIM(folds)
temp_fold=''
if (folds(j).neqv.':') then
temp_fold(j)=folds(j)
elseif (i.eq.1) then
read(temp_fold,*) FoldX
i=i+1
elseif (i.eq.2) then
read(temp_fold,*) FoldY
i=i+1
else
read(temp_fold,*) FoldZ
endif
enddo
当我编译这个我得到的错误:
When I compile this I get errors:
unfolder.f90(222):错误#6410:这个名字还没有被宣布为一个数组或一个函数。 [FOLDS]
unfolder.f90(222): error #6410: This name has not been declared as an array or a function. [FOLDS]
[stud2 @费曼vec2ascii] $如果(倍(J).neqv。':'),然后
附近意外的标记`Ĵ语法错误
[stud2 @费曼vec2ascii] $ ^ --------
[stud2@feynman vec2ascii]$ if (folds(j).neqv.':') then syntax error near unexpected token `j' [stud2@feynman vec2ascii]$ --------^
unfolder.f90(223):错误#6410:这个名字还没有被宣布为一个数组或一个函数。 [TEMP_FOLD]
unfolder.f90(223): error #6410: This name has not been declared as an array or a function. [TEMP_FOLD]
[stud2 @费曼vec2ascii] $ temp_fold(J)=倍(J)
[stud2@feynman vec2ascii]$ temp_fold(j)=folds(j)
附近意外的标记`Ĵ语法错误
syntax error near unexpected token `j'
我怎么能提取这些数字?
How can I extract those numbers?
推荐答案
您可以使用首页
固有功能定位第一个冒号的字符串中的位置,说 I
。然后,使用一个内部读取读取整数 X
从preceding子字符串:阅读(字符串(1:I-1), *)X
。然后将此过程的子串起始于 I + 1
获得是
。重复以Z
。
You can use the index
intrinsic function to locate the position in the string of the first colon, say i
. Then use an internal read to read the integer x
from the preceding sub-string: read (string (1:i-1), *) x
. Then apply this procedure to the sub-string starting at i+1
to obtain y
. Repeat for z
.
P.S。是你的错误消息,从bash而不是Fortran编译?
P.S. Are your error messages from bash rather than a Fortran compiler?
这篇关于在Fortran语言字符串中提取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!