You can store format specifications in character scalar references, numeric array references, or numeric scalar field references (see Section 2.2.5.1). Such format specifications are called run-time formats and can be constructed or altered during program execution.
A run-time format in an array has the same form as a FORMAT statement, without the word FORMAT and the statement label. Opening and closing parentheses are required. Variable format expressions are not permitted in run-time formats.
In the following example, the format specification changes with each iteration of the DO loop:
SUBROUTINE PRINT(TABLE) REAL TABLE(10,5) CHARACTER*5 FORCHR(0:5), RPAR*1, FBIG, FMED, FSML DATA FORCHR(0),RPAR /'(',')'/ DATA FBIG,FMED,FSML /'F8.2,','F9.4,','F9.6,'/ DO 20 I=1,10 DO 18 J=1,5 IF (TABLE(I,J) .GE. 100.) THEN FORCHR(J) = FBIG ELSE IF (TABLE(I,J) .GT. 0.1) THEN FORCHR(J) = FMED ELSE FORCHR(J) = FSML END IF 18 CONTINUE FORCHR(5)(5:5) = RPAR WRITE (6,FORCHR) (TABLE(I,J), J=1,5) 20 CONTINUE END
The DATA statement assigns a left parenthesis to the character array element FORCHR(0) and a right parenthesis and three field descriptors to four character variables for later use.
Next, the proper field descriptors are selected for inclusion in the format specification. The selection is based on the magnitude of the individual elements of the array TABLE.
A right parenthesis is then added to the format specification just before the WRITE statement uses it.