Format string for output dependent on a variable

If you are using Intel fortran, it has a proprietary extension for this -- you can include an existing variable in angle brackets to act as a specifier:

  write(*,'(3f15.3,<nvari>f9.2)') x,y,z,(var(i),i=1,nvari)

If you compiler supports it, '(3f15.3, *(f9.2))'

If you have an older compiler, just use a larger number than you will have items to output, e.g., '(3f15.3, 999(f9.2))'. You don't have to use up the format.

For the most complicated cases you can write a format to a string and use that as your format:

write (string, '( "(3f15.3, ", I4, "(f9.2))" )' )  nvari
write (*, string )  x,y,z, (array(i), i=1,nvari)

With the understanding of formats, including format reversion, the use of string formats is rarely necessary.


Instead of writing the format directly in the write statement, it's also possible to use a character variable.

character(len=32) :: my_fmt
my_fmt = '(3f15.3,3f9.2)'
write(*, my_fmt) x, y, z, (var(i), i = 1, nvari)

Now it is possible to manipulate the character variable to contain the wanted repeat count before the write statement, using a so-called internal write, or write to internal file.

write(my_fmt, '(a, i0, a)') '(3f15.3,', nvari, 'f9.2)'

(Just make sure the declared length of my_fmt is long enough to contain the entire character string.)