从Intel移植到GNU gfortran时的可变格式语句 [英] Variable format statement when porting from Intel to GNU gfortran

查看:94
本文介绍了从Intel移植到GNU gfortran时的可变格式语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在尝试写出如下所示的CSV文件标题:

Suppose I'm trying to write out a CSV file header that looks like this:

STRING1     2001,     2002,     2003,     2004,

还有一些实现此目的的可变格式Fortran90代码

And some variable-format Fortran90 code that does this is

INTEGER X<Y
X=2001
Y=2004
WRITE(6,'(A,(999(5X,I4,",")))') ' STRING1',(y,y=X,Y)

"999"重复(5X,I4,,")格式结构所需的次数(完成时至少重复999次).假设X和Y可能会发生变化,因此循环迭代次数也可能会发生变化.

The "999" repeats the (5X,I4,",") format structure as many times as it needs to (up to 999 times, at least) to complete. Assume X and Y are subject to change and therefore the number of loop iterations may also change.

但是如果我希望标题看起来像这样,则在序列的末尾添加一个额外的字符串,例如

But if I want the header to look like this, with an additional string on the end of the sequence, like

STRING1     2001,     2002,     2003,     2004, STRING2

...我尝试在格式字符串的末尾添加另一个A,但是这种重复的变量格式结构显然不知道在整数完成后需要转义",因此会出错.

...I have tried adding another A toward the end of the format string, but that repeated variable format structure apparently doesn't know that it needs to "escape" when the integers are done with and so it errors out.

我可以通过在格式字符串中包含'ADVANCE ="no"'并使用新的WRITE语句打印第二个字符串来获得我从根本上想要的东西来解决此问题,但是有一种方法可以通过单一格式的结构?

I can work around this by including 'ADVANCE="no"' in the format string and printing the second string using a new WRITE statement to get what I fundamentally want, but is there a way I can do this all with a single format structure?

[注意:请没有尖括号回答;这是针对GNU gfortran的,它不支持该扩展名]

推荐答案

来吧,亲爱的程序!

这是标准的Fortran 2008:

This is standard Fortran 2008:

WRITE(6,'(A,*(5X,G0,:,","))') ' STRING1',(y,y=X,Y), ' STRING2'

我相当确定gfortran支持不确定的组重复计数". G格式在Fortran 2008中进行了扩展,以支持任何内部数据类型,零宽度表示最少字符数".冒号是一种F77功能,可以阻止尾随逗号被发出.

I am fairly sure that gfortran supports the "indefinite group repeat count". G format was extended in Fortran 2008 to support any intrinsic data type, and a width of zero means "minimum number of characters." The colon is a F77 feature that stops the trailing comma from being emitted.

有了这个,ifort给了我

With this, ifort gives me:

 STRING1     2001,     2002,     2003,     2004,      STRING2

FWIW,对于将y用作循环控制变量,我不满意,因为这不是语句实体,并且在循环结束时将其设置为2005.请使用单独的变量!

FWIW, I am not happy with your reuse of y as the loop control variable, since this is NOT a statement entity and will get set to 2005 at the end of the loop. Use a separate variable, please!

这篇关于从Intel移植到GNU gfortran时的可变格式语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆