Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write 100 columns 1

Status
Not open for further replies.

GerritGroot

Technical User
Nov 3, 2006
291
ES
Hi,

Does anyone know how I can write a variable number of columns?

If the number is known, let’s say 100 I’d do:

------------------------------------------------
WRITE(1,FMT=’(100(E20.10))’)(myvar(i),i=1,100,1)
------------------------------------------------

But if the number is not known, let’s say it is imxcol, I cannot say:

------------------------------------------------------
WRITE(1,FMT=’(imxcol(E20.10))’)(myvar(i),i=1,imxcol,1)
------------------------------------------------------

Because the format descriptor doesn’t work anymore, the compiler doesn't take it at least.

So I tried in F90:
-----------------------------------------------
DO i=1,imxcol,1
WRITE(1,FMT=’(E20.10)’,ADVANCE=’NO’)myvar(i)
END DO
-----------------------------------------------

But at imxcol>85 the code breaks down!!

How can I solve this?

Thanks,

Gerrit
 
Create the format in a string first
Code:
character*32::varfmt
write (varfmt, 100) imxcol
100 format ('(',I0,'E20.10)')
write(1,varfmt) (myvar(i), i = 1, imxcol, 1)
 
Thank you XWB!!

It works fine, the idea of just making a string with the value is clear. The only thing neither me nor my compiler understood was the I0 zero width descriptor, but anyway the concept works fine

Thanks,

Gerrit

P.S.: Finally I did something like:

PROGRAM tst
IMPLICIT NONE

INTEGER i,imxcol
CHARACTER varfmt*13

imxcol=99
varfmt='(XXX(E20.10))'
WRITE(varfmt(2:4),FMT='(I3.3)')imxcol

OPEN(UNIT=1,FILE='tst.dat',STATUS='UNKNOWN')
WRITE(1,FMT=varfmt)(i*1.,i=1,imxcol,1)
CLOSE(1,STATUS='KEEP')

END
 
I0 gives a left justified number on my compiler. Sorry - it is a non standard thing. There may be another way of printing a left justified number with no trailing spaces. If I find out, I'll let you know.
 
Thanks again!

It doesn't matter so much I think, in F77 one may get around this using indices for strings like e.g. mynum(2:3) or so and finding the indices in some routine as a function of the number. After all, an integer is never going to be more characters than MAXINT (10 in my case)

Experimentating, formats like

---------------------
FMT='( 99(E20.10))'
---------------------

also worked on my compiler, as well as

---------------------
FMT='(000099(E20.10))'
---------------------

Regards,

Gerrit

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top