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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about arrays

Status
Not open for further replies.

Taiseer

Technical User
Jun 9, 2011
4
DE
Dear all,

I have a question about array and reading files. Actually I have always the same problem and I really need your help. Could you please show me how can read a text file containing a 2-d array.

How can I read the table without knowing the dimension of the array in it. How can I read it when I know its dimension. Is there any differences.

Thanks
 
What does your input data look like?

Are there any indicators in the file as to whether it is row column or column row?

Are there any inputs telling you the size of the array?

eg
0 <-- 0 = columns then rows, 1 = rows then columns
3 2 <-- means 3 columns 2 rows
1 2 3
4 5 6

Are there any indicators telling you the end of a row/column and the beginning of the next row (eg row ends with X)

1 2 3 X 4 5 6 X 7 8 9 X
 
Thank you,

actually I can know the number of columns and rows. I can open the table (text file) and there is four lines of header showing among other information columns and rows numbers.

now let us assume that I have 640 columns and 512 rows. How can I read the table as a 2d array of array1(nrow,ncol) and another array, let us say array2(ncol,nrow).

How can I read the the file as array3(ncolXnrow,1), and as array4(1,ncolXnrow).

Thanks
 
 http://www.mediafire.com/?nqglu280zf2vih1

Well, for as long as you know the number of rows and columns, you can put together a couple of explicite nested do-loops and read the data, like so:

allocate( a(nrows,ncols) )
do i = 1, nrows
do j = 1, ncols
read(*,*) a(i,j)
end do
end do

or, you could do one explicit loop and one implicit

allocate( a(nrows,ncols) )
do i = 1, nrows
read(*,*) (a(i,j), j=1,ncols)
end do

or, you could read the entire info into a 1-dimensional array, first, and then re-shape the data into a 2-D array, or 3...you just need to know how the data is lined up so that your re-shape data makes sense.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top