Delimit Fortran strings using numbers

时间:2017-06-12 16:55:09

标签: fortran delimiter fortran90 fortran95

I have an input file with letters and numbers that I'd like to delimit with numbers in Fortran 90/95. The input file looks like this:

AAAA (spaces) 123BBBB (spaces) 4CCCC (spaces) 5DDDD (spaces) -6EEEE

So on and so forth. I'd like for the numbers after the spaces to be with the four letters prior to the spaces. The problem I'm running into here is that the numbers can either be one, two, three, or four digits, and can have negative signs as well. I'm not sure how to automate delimiting in Fortran to get the appropriate numbers to the correct letters.

So far, I only have written a script which essentially replicates the input file and writes it to an output file. I wanted to accomplish this first before trying delimiting as above.

ALTERNATIVELY, I can try delimiting in Python (if it's easier in Python), and call the Python delimiting script from the Fortran program.

1 个答案:

答案 0 :(得分:2)

Fortran具有SCAN和VERIFY内在函数,可让您在指定字符集中(或不是)第一个(或可选的最后一个)字符的字符串中查找位置。您的示例格式不正确,因为EEEE之后没有数字,但我暂时忽略它。

我处理这个的方法是保持一个位置值,使用INDEX找到下一个空白,它告诉我当前位置有多少个字母。然后我会使用VERIFY设置'-0123456789'来识别下一个非数字。这告诉我下一个数字是什么。我将使用来自该子字符串的列表导向读取数字。重复直到字符串结束。

毫无疑问,有其他方法可以做到这一点,但完全没有必要使用另一种语言。