Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29273 Discussions

Q re purtting variables in Format statements

WSinc
New Contributor I
718 Views
Suppose I have a stiuation like this:

integer q(1:20),nq,nx
realx(1:20)
print 101,x(1:nx), q(1:nq)
101 format("x=",4F20.12," q=",4I10)

This works fine as long as nx=4, and nq is <= 4.
But if those values change, I want the FORMAT statement to reflect that.

The way I now handle that is to modify the FORMAT statment at execution time by
putting it in a CHARACTER array.

Has Fortran come up with a cleaner way to do this? I couldn't find anything.
0 Kudos
5 Replies
Steven_L_Intel1
Employee
718 Views
The Fortran standard doesn't have a cleaner way of doing what you want. In some circumstances, the Fortran 2008 "unlimited group repeat count" feature can help, but not here.

Intel Fortran supports an old extension that does what you want, called Variable Format Expressions. The syntax would be:

101 format("x=".F20.12,"q=",I10)

This extension is widely implemented, but not universally so.
0 Kudos
WSinc
New Contributor I
718 Views
Ok, I will try that. Thanks ! !
0 Kudos
Steven_L_Intel1
Employee
718 Views
No - what goes inside the <> is just an ordinary expression.
0 Kudos
IanH
Honored Contributor III
718 Views
For your specific example you could do something like the following, without having to resort to extensions:

[bash] WRITE (*, "('x=',*F20.12)", ADVANCE='NO') x(:nq) WRITE (*, "(' q=',*I10)") q(:nq)[/bash]
0 Kudos
Steven_L_Intel1
Employee
718 Views
Dang - I should have thought of that! Thanks, Ian!
0 Kudos
Reply