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

ARRAY data into VECTOR data

Status
Not open for further replies.

Aclems

Technical User
Mar 19, 2007
6
0
0
CA
Hi Guys,
I am trying to figure out a procedure using Fortran code but it is not working at all. I want to turn my array data in a file into vector data to be stored in another file, here is an example of what I am trying to do;
In File A; I have the following data,
234 245 256 220 221 234
234 345 234 123 222 222
112 213 321 234 112 134
And I want these data arranged as shown below in File B;
234 234 112 245 345 213 256 234 321 220 123 234 221 222 112 234 222 134.
i.e. column1->column2->column3->column4----etc

Please, your great suggestions as always are kindly appreciated.
-Aclems.
 
What about the following:

Code:
program rearrange

integer a(nlines,nvalue)   

! replace nlines with the number of lines you got
! and nvalue with the number of values per line

open (unit=20,file='input.dat',status='old')

do j1 = 1,nlines
  read (20,*)(a(j1,j2),j2=1,nvalue)
enddo

close (unit=20)

open(unit=21,file='ouput.dat')

do j2=1,nvalue
   write (21,*)(a(j1,j2),j1=1,nlines)
enddo

close (unit=21)

end

Of course there are ways to determine the number of lines during input and you should check if the open and read didn't fail, but this short prog should indicate how the rearrangement could be done.


Norbert

 
You can always write with advance='no'. That will stop newlines the system adding newlines after each number. For instance, if the output loop of the previous solution were changed to
Code:
do j2=1,nvalue
   write (21,*, advance='no')(a(j1,j2),j1=1,nlines)
enddo
Then everything would appear on the same line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top