16

I would like to have a Fortran write statement formatted to depend on some variable. For example, I could write:

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

where nvari = 3. But, what if, in some cases, I actually have 4 variables (i.e. nvari = 4). I would like to write something like this:

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

Now, nvari can be anything and the output will work as I like. How can I make something like this work?

2
  • 1
    Are 3 and 4 the only choices? Or are you looking for something that can handle any number?
    – sarnold
    Mar 26, 2012 at 23:21
  • 1
    OP specified 'nvari can be anything'.
    – pattivacek
    Sep 16, 2013 at 17:35

4 Answers 4

19

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)
6
  • Oh, cool. Thanks for that answer. That's exactly what I wanted. Apr 19, 2012 at 22:39
  • is there a way to do some mathematical operations on the <nvari> ? eg - <nvari>+2, or 3*<nvari> or <nvari>**2 in the format statement? i tried with a parentheses .. that did not work! EDIT: the workaround i use now is to define another integer variable nfmt and using it instead of nvari Oct 21, 2013 at 10:02
  • 1
    Is there a way to use A<my_int> style formatting using the gfortran compiler?
    – Chris
    Aug 29, 2017 at 11:44
  • Please see also other answers if you came looking for a solution, this answer will NOT work in gfortran and some other compilers. Feb 26, 2021 at 12:55
  • This helped me realize what the <param> was in a FORMAT statement I had. but now I have to switch compilers :(
    – Urchin
    Apr 13, 2021 at 21:50
15

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.

1
  • 1
    Cool. Thx. I am using the Intel Fortran Compiler, which I assume is pretty recent. But anyway, it doesn't do the '(3f15.3, *(f9.2))', so I'm using the second solution. BTW, what do you mean by "format reversion"? I did a bit of googling, but didn't come up with much. Mar 27, 2012 at 16:28
13

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.)

4
  • 1
    Or, wrt the last (parenthetical) statement, use an allocatable-lenght character variable. Mar 27, 2012 at 4:27
  • @HighPerformanceMark: Sure, if your compiler supports it, that would be the best solution.
    – eriktous
    Mar 27, 2012 at 10:57
  • 1
    For those using the gfortran compiler, this solution is the recommended solution in the gfortran documentation. See "Variable FORMAT expressions" in the documentation (section 6.2.3 for gfortran 4, gfortran 5; section 6.2.2 for gfortran 6). May 26, 2017 at 19:38
  • I caution against writing to strings. Gfortran on Mac is not thread safe for writing to string variables. I/O to files does seem to be thread safe, just not strings.
    – Jason
    Sep 1, 2017 at 15:34
4

You wanted to write something like this:

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

In fact, there is an old trick in the Fortran standard that allows you to omit the nvari, thus:

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

or even thus:

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

The standard says that the last descriptor in the format is implicitly repeated as often as is necessary to accommodate all of the variables in the list. That 'last descriptor' could be parenthesized such that the last group of descriptors is implicitly repeated, for example:

write(*,'(3f15.3,(2x,f9.2))') x, y, z, (var(i), i=1,nvari)
1
  • I am using gfortran9. This didn't work for me unless I used parentheses as in your second example. It also produced the repeated output on new lines rather than on the same line, which is not what the OP wanted.
    – Mead
    Dec 3, 2019 at 14:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.