1

I have found this weird statement in a piece of code:

read(10, *) ((matrix(i, j), i=1, n), j=m, 1, -1)

I am wondering how this inline recursive reading works. What is the meaning of ((matrix(i, j), i=1, n), j=m, 1, -1)?

nbro
  • 15,395
  • 32
  • 113
  • 196
emanuele
  • 2,519
  • 8
  • 38
  • 56

1 Answers1

4

This is not an in-line recursive read (not sure where you got this term from), this is an example of a nested implied do loop, see here, for example, for the syntax of an implied do loop and many examples of these in action. Basically an implied do loop is a way to write a do loop on a single line. With nested implied do loops you can write multiple do loops on a single line.

In your case, what you have is equivalent to (someone please correct me here if there there are any differences the OP should be aware of) something like (notice that I have unravelled the implied do loop from the outer loop inwards):

integer, parameter :: n=<some-value>
integer, parameter :: m=<some-value>
<some-type>, dimension(n,m) :: matrix

integer :: i, j

do j = m,1,-1
  do i = 1,n
      read(10,*) matrix(i,j)
  end do
end do
Chris
  • 44,602
  • 16
  • 137
  • 156
  • 4
    There is one imortant difference, in that the implied do-loop version is just one read statement (eg, it doesn't try to read m*n lines), whereas the version above loops over m*n reads so that it's m*n lines. However, other than that, the explanation is correct; the implied do-loop is concise notation for expressing something similar to do loops but "in place". – Jonathan Dursi Oct 28 '12 at 17:36
  • 1
    I would add to what Jonathan Dursi has already said, that an implied do-loop in a `read` statement transfers all data, in Fortran terms, from a _single record_ (e.g. a single line of input). An implied do-loop in a `write` statement transfers all data to a _single record_ (e.g. a single line of output). – Hristo Iliev Oct 28 '12 at 23:03
  • 1
    There is another difference: performance. In [this](http://stackoverflow.com/questions/12567087/implied-do-vs-explicit-loop-with-io) question it was found that I/O was significantly slower in explicit loops than in implied ones or simple I/O statements like `write(10, *) matrix`. This is, however, compiler-dependent, and probably only really significant when you have to read/write a large amount of data. – sigma Oct 29 '12 at 16:09