10

How to print many values without line breaks?

PRINT *, "foo:", foo, ", bar:", bar, ", baz:", baz

Apparently, this is possible with WRITE (here and there). How to achieve the same with PRINT and its different syntax while printing several values?

Codoscope
  • 892
  • 1
  • 10
  • 18
  • Are you asking how to specify an explicit format when using `print`? – francescalus Aug 31 '17 at 16:50
  • 2
    Print is basically a less-customizable version of write. Why are you attempting to do this? – Ross Aug 31 '17 at 17:27
  • 1
    Just use write, print is just for simple stuff. – Vladimir F Героям слава Aug 31 '17 at 19:08
  • Is `print` just a shorthand for `write`, even with Fortran90? Yet, `write(*,*,advance='no') "foo:", foo, ", bar:", bar, ", baz:", baz` doesn't work for me. It says: `error #6568: This use of the ADVANCE, SIZE, or EOR specifier is invalid.` I don't think that it's the same issue as [this one](https://stackoverflow.com/questions/24377521/non-advancing-read-in-fortran-with-free-format), which tries to read. – Codoscope Sep 01 '17 at 08:26

1 Answers1

16

The write statement provides an optional advance specifier, but print does not.

Multiple write statements with advance="no" can be made at different places in your code in order to print multiple items to the same line. Just as an example, using it from within a do loop:

do i=1,3
    write(*, fmt="(1x,a,i0)", advance="no") "loop #", i
end do
write(*,*) ! Assumes default "advance='yes'".
write(*,*) "--OK, the loop is done!"

! Example output:
 loop #1 loop #2 loop #3
 --OK, the loop is done!

Note that advance can't be used with list-directed output (using the "*" to "print anything"). Therefore, I've shown an example format specifier fmt="(1x,a,i0)" which will print a single blank space, a character string, and a single integer for each write statement. A language reference and/or your compiler documentation comes in handy. See, here, for example.

As others have suggested, if this is the desired behavior, it's best to use write. If for some reason you still prefer to use print, then you should probably assemble your output items into a single variable or list of variables before printing it.

Matt P
  • 2,287
  • 1
  • 11
  • 26
  • 1
    The following can be useful to learn more about the format descriptor: http://www.chem.ox.ac.uk/fortran/format.html, although I haven't found anything to display matrices without loops like `PRINT` offers. – Codoscope Sep 08 '17 at 09:23
  • @Qu'est-cet'yont Hmm, I'm not sure what you mean by that. Could you show an example of what `print` can do that `write` does not, possibly in your original post? – Matt P Sep 08 '17 at 09:45
  • @MattP I have a lot of variables that I am trying to print one line and they have to separated by commas, to create a .csv file. Is there a more practical solution than a loop? – Herman Toothrot Mar 05 '19 at 07:38
  • @HermanToothrot That might be a good new question. Everything on one line, with comma separation? Sure, absolutely. I recommend you look into implied do-loops, and the 'unlimited repeat' format specifier. (The same ideas can be used even for multiple lines, if you feel like you really need a one-liner, but the syntax looks a little tricky unless you're used to it. ) – Matt P Mar 05 '19 at 15:53