在Fortran中写出数组时控制换行 [英] Controlling newlines when writing out arrays in Fortran

查看:69
本文介绍了在Fortran中写出数组时控制换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些代码基本上可以做到这一点:

So I have some code that does essentially this:

REAL, DIMENSION(31) :: month_data
INTEGER :: no_days
no_days = get_no_days()
month_data = [fill array with some values]
WRITE(1000,*) (month_data(d), d=1,no_days)

所以我有一个每个月都有值的数组,在循环中,我根据该月有多少天用一定数量的值填充数组,然后将结果写到文件中.

So I have an array with values for each month, in a loop I fill the array with a certain number of values based on how many days there are in that month, then write out the results into a file.

我花了相当多的时间将头全部包裹在WRITE的一次性编写数组"方面,但这似乎行得通.

It took me quite some time to wrap my head around the whole 'write out an array in one go' aspect of WRITE, but this seems to work.

但是,它会像这样写出数组中的数字(例如一月的示例,所以有31个值):

However this way, it writes out the numbers in the array like this (example for January, so 31 values):

  0.00000         10.0000         20.0000         30.0000         40.0000         50.0000         60.0000
  70.0000         80.0000         90.0000         100.000         110.000         120.000         130.000
  140.000         150.000         160.000         170.000         180.000         190.000         200.000
  210.000         220.000         230.000         240.000         250.000         260.000         270.000
  280.000         290.000         300.000

因此,它给很多空格加上前缀(即使数组中有更大的值,也可能使列对齐),并且包装行以使其不超过特定宽度(我认为128个字符?不确定).

So it prefixes a lot of spaces (presumably to make columns line up even when there are larger values in the array), and it wraps lines to make it not exceed a certain width (I think 128 chars? not sure).

我真的不介意多余的空间(尽管它们会使我的文件大小大大增加,所以也可以解决这个问题……),但是分手行却弄乱了我的其他工具.我曾尝试阅读几本Fortran手册,但是虽然其中一些提到输出格式",但我还没有找到提到换行符或列的手册.

I don't really mind the extra spaces (although they inflate my file sizes considerably, so it would be nice to fix that too...) but the breaking-up-lines screws up my other tooling. I've tried reading several Fortran manuals, but while some of the mention 'output formatting', I have yet to find one that mentions newlines or columns.

那么,在Fortran中使用上述语法时,如何控制如何写出数组?

So, how do I control how arrays are written out when using the syntax above in Fortran?

(另外,当我们使用它时,如何控制十进制数字的nr?我知道这些都是整数值,所以我想将所有的十进制数都省略掉,但是我不能更改出于某种原因,在我的代码中将数据类型设置为INTEGER).

(also, while we're at it, how do I control the nr of decimal digits? I know these are all integer values so I'd like to leave out any decimals all together, but I can't change the data type to INTEGER in my code because of reasons).

推荐答案

您可能想要类似的东西

WRITE(1000,'(31(F6.0,1X))') (month_data(d), d=1,no_days)

说明:

  • 使用 * 作为格式规范称为列表定向I/O:易于编码,但是您将对格式的所有控制权交给了处理器.为了控制格式,您需要通过 FORMAT 语句的标签或字符变量来提供显式格式.
  • 对小数形式的实变量使用 F 编辑描述符.它们的语法为F w . d ,其中 w 是字段的宽度,而 d 是字段的数量小数点后一位,包括小数点.因此, F6.0 表示一个长度为6个字符的字段,没有小数位.
  • 可以使用 X 控件编辑描述符添加空格.
  • 可以用符号前的重复次数来表示编辑描述符的重复.
  • 可以使用( ... )创建组,并且如果前面有许多重复,可以将它们重复.
  • 除了最后一个提供的变量之外,没有其他项目可以打印,即使格式指定了比实际提供的项目更多的打印方式,因此即使您使用了几个月的时间,您也可以要求重复 31 只会打印30或28天的数据.
  • The use of * as the format specification is called list directed I/O: it is easy to code, but you are giving away all control over the format to the processor. In order to control the format you need to provide explicit formatting, via a label to a FORMAT statement or via a character variable.
  • Use the F edit descriptor for real variables in decimal form. Their syntax is Fw.d, where w is the width of the field and d is the number of decimal places, including the decimal sign. F6.0 therefore means a field of 6 characters of width with no decimal places.
  • Spaces can be added with the X control edit descriptor.
  • Repetitions of edit descriptors can be indicated with the number of repetitions before a symbol.
  • Groups can be created with (...), and they can be repeated if preceded by a number of repetitions.
  • No more items are printed beyond the last provided variable, even if the format specifies how to print more items than the ones actually provided - so you can ask for 31 repetitions even if for some months you will only print data for 30 or 28 days.

此外,

  • 可以在/控件编辑描述符中添加新行;例如,如果您要打印每行10个值的数据,则可以

  • New lines could be added with the / control edit descriptor; e.g., if you wanted to print the data with 10 values per row, you could do

WRITE(1000,'(4(10(F6.0,:,1X),/))') (month_data(d), d=1,no_days)

  • 在第二个示例中注意:控件编辑描述符:它指示,如果没有更多可打印的项目,则不应该打印其他任何内容-甚至不包含与控件编辑相对应的空格描述符,例如 X /.尽管可以在前面的示例中使用它,但在此处更相关,以确保如果 no_days 10 的倍数,则不会3行数据后的空行.

  • Note the : control edit descriptor in this second example: it indicates that, if there are no more items to print, nothing else should be printed - not even spaces corresponding to control edit descriptors such as X or /. While it could have been used in the previous example, it is more relevant here, in order to ensure that, if no_days is a multiple of 10, there isn't an empty line after the 3 rows of data.

    如果要完全删除小数点符号,则需要使用 nint 内在函数和I w (整数)来打印最接近的整数.描述符:

    If you want to completely remove the decimal symbol, you would need to rather print the nearest integers using the nint intrinsic and the Iw (integer) descriptor:

    WRITE(1000,'(31(I6,1X))') (nint(month_data(d)), d=1,no_days)
    

  • 这篇关于在Fortran中写出数组时控制换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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