The colon edit descriptor : is used to terminate format scanning immediately if there is no more data items in the READ and WRITE list. The form of this descriptor is as follows:
Format scanning starts with 1X, then proceeds to I5. After a data item is processed using I5, Fortran encounters :. If there is no unprocessed data items, format scanning stops right at this : without further processing. If there are unprocessed data items, format scanning continues with T20 and F5.2. After the data item is processed with F5.2, the next one is :. If there is no unprocessed data items, format scanning stops here. Otherwise, format scanning continues with / which terminates the current input or output line. The next input or output line is processed with T10 followed by F5.0.(1X, I5, :, T20, F5.2, : / T10, F5.0 )
If there is no colons like the following:
then data items are processed with 1X, followed by I5. After processing the data item using I5, if there is no unprocessed data items, format scanning continues to T20 and then encounters F5.2. Since there is no data items, the input/output activity stops there. So, the current position is moved to position 20. On the other hand, if a : is placed after I5, format scanning stops after I5. Well, there is no difference in this case.(1X, I5, T20, F5.2 / T10, F5.0 )
If there are unprocessed data items, F5.2 is used and then the current line is terminated followed by T10. Now the available position for reading and printing is moved to position 10 on the next line. If at this point there is no unprocessed data item, we are on the second line. However, once a READ or a WRITE completes, Fortran always ignores the remaining content on the current line. In this case, the next READ or WRITE starts with the third line! If there is a colon after F5.2, format scanning stops while we are still on the first line and the next READ or WRITE starts with the second line. Therefore, the colon edit descriptor can prevent line advancing.
This is a perfect format. After printing five integers, we see a slash which terminates the current output line. Then, format scanning continues and sees the closing right parenthesis. This would produce a blank line before going back to the beginning of format to print the next five integers. But, it may not do the job. Let us put this format to test. The following is a simple program (Click here to download a copy):(1X, 5I3 / )
If we input 15 for variable n, we have the following result:PROGRAM ColonTest IMPLICIT NONE INTEGER :: i, n CHARACTER(LEN=15), PARAMETER :: DashLine = "---------------" READ(*,*) n WRITE(*,"(1X,5I3/)") (i, i = 1, n) WRITE(*,"(1X,a)") DashLine WRITE(*,"(1X,5I3:/)") (i, i = 1, n) WRITE(*,"(1X,a)") DashLine END PROGRAM ColonTest
It is clearly shown by the above output that after the fifteenth number is printed, format scanning continues and sees the / before encountering the closing right parenthesis. As a result, we have an extra blank line! However, if we add a colon between 5I3 and / so that after the last number on an output line is printed and there is no unprocessed data items, format scanning stops. Thus, after the fifteenth data item is printed, format scanning encounters : and stops there. As a result, no extra blank line is printed.1 1 2 ....5....0....5....0 1 2 3 4 5 <----+ | 6 7 8 9 10 +-- printed by the first WRITE (no colon) | 11 12 13 14 15 | <----+ --------------- 1 2 3 4 5 <----+ | 6 7 8 9 10 +-- printed by the second WRITE (with colon) | 11 12 13 14 15 <----+ ---------------
Ok, you might want to say: I don't care the extra blank line because it does no harm. Yes, it is true. So, let us use 13 for variable n and here is the result:
Now the blank line disappears. Why? Because only thirteen integers are printed, after the thirteenth one is printed, the next available edit descriptor is I5 and consequently format scanning stops there with no chance to encounter the /. The result is that no extra blank line is printed.1 1 2 ....5....0....5....0 1 2 3 4 5 <----+ | 6 7 8 9 10 +-- printed by the first WRITE (no colon) | 11 12 13 <----+ --------------- 1 2 3 4 5 <----+ | 6 7 8 9 10 +-- printed by the second WRITE (with colon) | 11 12 13 <----+ ---------------
This teaches us a lesson: When you want to stop format scanning early, use the colon edit descriptor. This is commonly used to fine tune the input/output for the last line.