Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I have what I hope is a simple question in fortran 95 is there a simple way to read in data of the following form into an array? N 2 M 4 1.2 3.4 5.6 7.8 9.0 2.3 6.5 8.7 Ideally I'd like to read in N, M (I can do that) allocate an array such that it has dimensions a(N,M) and then read in the values below into the array like a(1,1) a(1,2) a(1,3) a(1,4) a(2,1) a(2,2) a(2,3) a(2,4) I can't see how best to set up a read statement to fill in the array when I have to read in the value of M at run time. Any thoughts Thanks Glyn
Post Follow-up to this message"Glyn Edwards" <glynedwards@fastmail.fm> wrote in message news:pan.2004.10.22.09.57.53.905655@fastmail.fm... > > Ideally I'd like to read in N, M (I can do that) allocate an array such > that it has dimensions a(N,M) and then read in the values below into > the array like > Well, that is perfectly straightforward. Since we have to assume that this is a homework assignment, perhaps you could post your attempt for comment, rather than have us do it for you. Regards, Mike
Post Follow-up to this messageOn Fri, 22 Oct 2004 11:09:08 +0100, Michael Metcalf wrote: > Well, that is perfectly straightforward. Since we have to assume that this > is a homework assignment, perhaps you could post your attempt for comment, Heh heh heh, unfortunately it isn't a homework assignment it is just an extension that I'd like to make to a program I'm writing for my PhD. However, I don't particularly want to spend lots of time on it. Ideally I'd like to read in a line from the file and then parse through this number by number. I'm not sure what the best way to do this is though and I'd like to avoid hacked solutions. I can do it in perl very easily but unfortunately I have to do it in fortran Glyn
Post Follow-up to this messageOn Fri, 22 Oct 2004 11:09:08 +0100, Michael Metcalf wrote: > is a homework assignment, perhaps you could post your attempt for comment, I'd like to do it like this read(11, fmt=*) a(n,1:M) But that doesn't work Glyn P.S. Still enjoying the homework comment, shows my level of Fortran really : -)
Post Follow-up to this message"Glyn Edwards" <glynedwards@fastmail.fm> wrote in message news:pan.2004.10.22.10.41.29.39207@fastmail.fm... > On Fri, 22 Oct 2004 11:09:08 +0100, Michael Metcalf wrote: > > > I'd like to do it like this > > read(11, fmt=*) a(n,1:M) > > But that doesn't work > > Glyn > > P.S. Still enjoying the homework comment, shows my level of Fortran really > :-) > Your read doesnt specify the complete array, try read (11,*) a Just to be sure list processing doesnt have problems with your file's format I whipped up a test program below. ! ------------------------ program test integer :: n, m real,allocatable :: a(:,:) character :: c open (11,file='test.txt') ! create test file write (11,'(a)') 'N 2', 'M 4', & '1.2 3.4 5.6 7.8', '9.0 2.3 6.5 8.7' rewind (11) read (11,*) c,n, c,m ! input array dimensions allocate ( a(n,m) ) read (11,*) a write (*,*) a ! outputs: 1.2 3.4 5.6 7.8 9.0 2.3 6.5 8.7 end program
Post Follow-up to this message> > Your read doesnt specify the complete array, try read (11,*) a > > Just to be sure list processing doesnt have problems with your file's form at > I whipped up a test program below. Many thanks your program does just what I was after. Coincidently I managed to find the following way of reading it in as well, which still reads in line by line. I didn't realise read could read across lines for a 2D array do n=1,nn read (11,fmt=*,iostat=ios) (array(n,m),m=1,mm) end do Thanks for all your help Glyn
Post Follow-up to this messageMethinks you wanted the array organized a(m,n) instead of a(n,m) but note it matters not if the complete array is read (11,*) a all the data is input. "Glyn Edwards" <glynedwards@fastmail.fm> wrote in message news:pan.2004.10.22.11.56.37.611884@fastmail.fm... > > Many thanks your program does just what I was after. Coincidently I > managed to find the following way of reading it in as well, which still > reads in line by line. I didn't realise read could read across lines for a > 2D array > > do n=1,nn > read (11,fmt=*,iostat=ios) (array(n,m),m=1,mm) > end do > > Thanks for all your help > > Glyn On 2nd look my test program shud have allocated array a(m,n) but no big deal, if you decide it isnt ordered the way you want, you can reorder it with 1 statement, e.g. a(n,m) to a(m,n) a = reshape( a, [m,n], [2,1] )
Post Follow-up to this messageGlyn Edwards <glynedwards@fastmail.fm> wrote in message news:<pan.2004.10.22.09.57.53.90565
5@fastmail.fm>...
> Hi, I have what I hope is a simple question
>
> in fortran 95 is there a simple way to read in data of the following form
> into an array?
Well ... let me be nice once.
program read_array
implicit NONE
integer :: i
integer :: N = 2 , M = 4
real, allocatable, dimension(:, :) :: a
open(11, file = "somefile_name" )
read(11, *) n, m !
allocate(a(n, m))
read(11, *) (a(i, :), i = 1, n)
print(*, "('>', 4F5.1)") (a(i, :), i = 1, n)
end program read_array
! produces :
> 1.2 3.4 5.6 7.8
> 9.0 2.3 6.5 8.7
> Ideally I'd like to read in N, M (I can do that) allocate an array such
> that it has dimensions a(N,M) and then read in the values below into
> the array like
> a(1,1) a(1,2) a(1,3) a(1,4)
> a(2,1) a(2,2) a(2,3) a(2,4)
>
> I can't see how best to set up a read statement to fill in the array when
> I have to read in the value of M at run time.
[JvO]
Post Follow-up to this message"David Frank" <dave_frank@hotmail.com> wrote in message news:zi6ed.13827$1f.5151@tornado.tampabay.rr.com... > read (11,*) a > write (*,*) a ! outputs: 1.2 3.4 5.6 7.8 9.0 2.3 6.5 8.7 Your array is in transposed order, however. You could fix this via: a = reshape(a,shape(a),order=(/2,1/)) but it would likely be more efficient to read the elements into the array in the desired order in the first place. -- write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, & 6.0134700243160014d-154/),(/'x'/)); end
Post Follow-up to this messageGlyn Edwards <glynedwards@fastmail.fm> wrote in message news:<pan.2004.10.22.09.57.53.90565 5@fastmail.fm>... > Hi, I have what I hope is a simple question > > in fortran 95 is there a simple way to read in data of the following form > into an array? > > N 2 > M 4 > > 1.2 3.4 5.6 7.8 > 9.0 2.3 6.5 8.7 > > Ideally I'd like to read in N, M (I can do that) allocate an array such > that it has dimensions a(N,M) and then read in the values below into > the array like > > a(1,1) a(1,2) a(1,3) a(1,4) > a(2,1) a(2,2) a(2,3) a(2,4) > > I can't see how best to set up a read statement to fill in the array when > I have to read in the value of M at run time. David Frank and JvO have answered your question. One thing to beware of is that if you try to read 4 numbers from a line but there are fewer than 4, the remaining numbers will be read from the next line. A problem with the data file may be hidden in this way. To avoid this, I usually read each line into a string using the "(a)" format and then read the numbers from this string.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.