Implied Do Loops in READ and WRITE statements1
Implied Do Loops in read and write statements
If you have something like the following:
REAL :: A(10)
DO I=1,10
READ (5,*) A(I)
END DO
This will read one number, A(I), for each execution of the read; hence one number per record is read no matter how many numbers are on the record. Each execution of the read starts on a new record! Total of 10 records read. ACTUALLY, IF A RECORD WERE BLANK, THE READ WOULD GO ON TO THE NEXT RECORD, SO MIGHT READ MORE THEN 10 RECORDS.
Now the "implied do":
REAL :: A(10)
READ (5,*) (A(I),I=1,10)
The "implied do" in the read says to read A(I) with I going from 1 to 10. This will read 10 values, and will read as records as necessary in order to read the 10 values. If there are 3 numbers per record, will read a total of 4 records. Even though each execution of a read starts on a new record, the READ is only executed 1 time. The implied loop is saying to read A(I) for I going from 1 to 10.
NOTE: the parenthesis are REQUIRED around the variable to be read and the implied do. Implied do loops can ONLY BE USED IN READ AND WRITE STATEMENTS.
You can nest the implied dos:
REAL :: A(10,10)
READ (5,*) ((A(I,J),J=1,10),I=1,10)
The I loop is considered the outside loop, and J the inner loop. Hence the above reads by rows: A(1,1), A(1,2), ..., A(1,10), A(2,1), A(2,2) ... Will read as many records as necessary to read 100 numbers. If there are 6 numbers per record, a total of 17 records will be read. You MUST know how the numbers are arranged in the data file; i.e., in this case they must be listed by rows.
Suppose you had,
REAL :: A(10,10)
DO I=1,10
READ (5,*) (A(I,J),J=1,10)
END DO
This would read the matrix by rows, with EACH ROW ALWAYS STARTING ON A NEW RECORD. Why? Because, for each value of I (which is the row variable), the READ is executed once. If there are a max of 6 numbers per record, then for I=1, reads first row (6 on first record, 4 on second record). For I=2, second row (6 on next record, 4 on next record). etc.
You can have more then one variable in the implied loop. For example,
READ (5,*) (A(I),B(I),I=1,10)
will read 20 values in the following order: A(1), B(1), A(2), B(2), etc. How many records are read depends on many numbers are on each record (and it does not have to be same number per record); in other words, values are read until the read is "satisfied", i.e., until 20 values are found.
Formatting can change things. If you have,
READ (5,15) (A(I),B(I),I=1,10)
15 FORMAT (2F10.3)
This will read 2 values per record (cols 1-10, 11-20) no matter how many numbers are on the record OR WHERE THEY ARE PLACED!
Implied do can be used in WRITE statements also. For example
REAL :: A(10,10)
!… some program statements
DO I=1,N
WRITE (99,10) I, (A(I,J),J=1,M)
10 FORMAT (" row", I5,/,(9X,5E15.5))
END DO
Writes matrix out by rows. Each row starts on a new line followed the values in that row (5 values per line). Suppose M=8, N=3, output is given by:
row 1
0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx
0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx
row 2
0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx
0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx
row 3
0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx
0.xxxxxE xx 0.xxxxxE xx 0.xxxxxE xx
What happens in a READ if you don't put any subscripts on a subscripted variable in the READ statement? Reads whole array by columns!!
REAL::A(10,10)
READ (5,*) A
This will read the values (by columns) until 100 values are read. Unless this is what you want to do, this can lead to BIG MISTAKES!! Formatting could change number of records read.
I suggest that you NEVER DO A READ OF A SUBSCRIPTED VARIABLE IN THIS WAY. Why? Suppose you only wanted to input a 5x5 array, but you have it dimensioned 10x10. If you put READ (5,*) A, it will try to read 100 values even though you only wanted to input 25 values!!!!