replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Using variables in format statements
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using variables in format statements

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
aerotex



Joined: 05 Jul 2007
Posts: 10

PostPosted: Wed Nov 05, 2008 5:53 pm    Post subject: Using variables in format statements Reply with quote

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?
Back to top
View user's profile Send private message
JohnHorspool



Joined: 26 Sep 2005
Posts: 270
Location: Gloucestershire UK

PostPosted: Wed Nov 05, 2008 8:26 pm    Post subject: Reply with quote

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.

Code:

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
Back to top
View user's profile Send private message Visit poster's website
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Thu Nov 06, 2008 2:42 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
IanLambley



Joined: 17 Dec 2006
Posts: 506
Location: Sunderland

PostPosted: Thu Nov 06, 2008 1:45 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
aerotex



Joined: 05 Jul 2007
Posts: 10

PostPosted: Wed Dec 03, 2008 10:29 am    Post subject: Reply with quote

Many thanks for all your help. Problem now sorted.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group