12

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?

4
  • Are you asking how to specify an explicit format when using print? Commented Aug 31, 2017 at 16:50
  • 2
    Print is basically a less-customizable version of write. Why are you attempting to do this? Commented Aug 31, 2017 at 17:27
  • 1
    Just use write, print is just for simple stuff. Commented Aug 31, 2017 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, which tries to read. Commented Sep 1, 2017 at 8:26

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The following can be useful to learn more about the format descriptor: chem.ox.ac.uk/fortran/format.html, although I haven't found anything to display matrices without loops like PRINT offers.
@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?
@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?
@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. )

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.