Reputation: 818
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
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
This would allow to write 5 integers of width 6 and when done, go automatically to the next line. Therefore, it is quite useful for formatting.
The question is if this can be done in reverse
read(*,'(5I6)') (a(i),i=1,20)
This would of course not work, as it is written, because there are only 5 elements per line. Never the less I would like to ask how to flip to the next line in an automatic mode. I am currently counting the number of entries and once 5 is reached, I go to the next line. Another option would be to read the file in a binary scratch file, where no 'new' lines exist. This would not work neatly for me, because I use keywords search with the index()
function.
Upvotes: 1
Views: 702
Reputation: 32451
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
Upvotes: 1
Reputation: 60088
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)
Upvotes: 1