 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
aerotex
Joined: 05 Jul 2007 Posts: 10
|
Posted: Wed Nov 05, 2008 5:53 pm Post subject: Using variables in format statements |
|
|
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 |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Wed Nov 05, 2008 8:26 pm Post subject: |
|
|
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 |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Thu Nov 06, 2008 2:42 am Post subject: |
|
|
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 |
|
 |
IanLambley
Joined: 17 Dec 2006 Posts: 506 Location: Sunderland
|
Posted: Thu Nov 06, 2008 1:45 pm Post subject: |
|
|
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 |
|
 |
aerotex
Joined: 05 Jul 2007 Posts: 10
|
Posted: Wed Dec 03, 2008 10:29 am Post subject: |
|
|
Many thanks for all your help. Problem now sorted. |
|
Back to top |
|
 |
|
|
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
|