Print values without new line

Codoscope picture Codoscope · Aug 31, 2017 · Viewed 9.1k times · Source

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?

Answer

Matt P picture Matt P · Sep 1, 2017

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.