Formatted reads and implied do

前端 未结 2 547
无人及你
无人及你 2021-01-25 19:19

In Fortran one has the option to use implied loops. They are usually used for printing and have the following structure.

write(*,\'(5I6)\') (i,i=1,20)

! output          


        
相关标签:
2条回答
  • 2021-01-25 19:25

    Format reversion is the key term here. It applies to both output and input.

    Before going further, the implied-do is largely irrelevant, so I'm going to stick to data transfer statements with whole arrays. That's true for the first output where it may as well be an array with the values 1 to 20 rather than an implied-do (try it!). So,

    read (*,fmt) a
    

    is just like

    read (*,fmt) (a(i),i=1,20)
    

    when a has those bounds.

    For output, the going "automatically to the next line" is part of processing output with format reversion. Once the format items have been exhausted and there remain more items to write out we go back to the start of the format. We also move onto the start of the next record. In this case, that's the start of a new line. Which gives the output

     1     2     3     4     5
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20
    

    After every fifth item of the twenty we start a new line.

    Format reversion is the same when reading. Given

    read(*,'(5I6)') a
    

    five elements of a are transferred according to the 5I6 part of the format. At that point the format item list is exhausted and format reversion occurs. Back to the start of the format and on to the next record.

    To conclude

    read(*,'(5I6)') a
    

    is the reverse of

    write(*,'(5I6)') a
    
    0 讨论(0)
  • 2021-01-25 19:28

    I would do it like this if you really need the format for some reason

    do i = 1, n, 5
      read(*,'(5I6)') a(i:i+4)
    end do
    

    otherwise the list directed format would work well as suggested by High Performance Mark.

    You could write it as an implied do, but sub-arrays are better.

    I could even imagine a one liner with a format descriptor including /, but why bother?

    (too long for a comment)

    0 讨论(0)
提交回复
热议问题