Silverfrost Forums

Welcome to our forums

Using variables in format statements

5 Nov 2008 4:53 #3963

I have an integer variable NUM_PTS. I want to read 'NUM_PTS' instances of another variable from a text file (all on the same line) using a format statement. I thought this was achieved by:

DO I = 1, NUM_PTS READ(10,120) INP_VAR(I) ENDDO 120 FORMAT (<NUM_PTS>(I3,2X))

But this does not seem to work. Any ideas how I can achieve this?

5 Nov 2008 7:26 #3966

It was achieved like that on VAX with VMS, however this was one of those non-standard extensions of which there were quite a few on VAX/VMS, and as such being not part of the fortran77 standard will not migrate to FTN95.

Your problem is easily overcome using a character string variable for the formatting.

character*11 form

form='(  (I3,2X))'


write(form(2:3),'(I2)')NUM_PTS

DO I = 1, NUM_PTS 
READ(10,form) INP_VAR(I) 
END DO
6 Nov 2008 1:42 #3967

You have the read in a do loop, implying 1 value per line. Your question implies the values are all on the one line.

If all the point values are on the same line, why not try

READ(10,120) (INP_VAR(I),i=1,num_pts) 120 FORMAT (bn,120(I3,2X))

ie read as many that appear on the same line (assuming 120 is more than expected)

if num_pts is on the same line then

READ(10,120) num_pts, (INP_VAR(I),i=1,num_pts)

is also valid.

John

6 Nov 2008 12:45 #3974

How about just:

READ(10,) num_pts read(10,) (INP_VAR(I),i=1,num_pts)

or

READ(10,*) num_pts, (INP_VAR(I),i=1,num_pts)

The inp_var values can be on one or many lines, separated by spaces or commas

Regards

Ian

3 Dec 2008 9:29 #4060

Many thanks for all your help. Problem now sorted.

Please login to reply.