Comparing line in one file to multiple lines in another/FORTRAN

In summary, gsal suggests reading the entire file at once instead of line by line, and declaring arrays for the xxxx1 variables so that they can be accessed easily.
  • #1
maria4376
2
0
Hi y'all

I am trying to compare two files, so make one line from one file be compared to alll the lines in the other file until it finds a match where the difference is less than 0.003.

This what I had thought of, but I still think it is just comparing line by line, instead of one line to all the other lines. Any suggestions would be great!

This is what I have so far:


program matchCoords
implicit none

real :: xcoord1, ycoord1,x1,y1,mag1,merr1,sharp1,chi1
real :: xcoord2, ycoord2,x2,y2,mag2,merr2,sharp2,chi2
real, parameter :: arcsec = 0.0003
character (len=50) :: inputfile1, inputfile2, outputfile
integer :: i

!Open input file as unit 15
write(*,*) 'Enter inputfile #1:'
read(*,*) inputfile1
write(*,*) 'Enter inputfile #2'
read(*,*) inputfile2
write(*,*) 'Enter output file name:'
read(*,*) outputfile
open(unit=15, file=inputfile1, status='old')
open(unit=16, file=inputfile2, status='old')
open(unit=17, file=outputfile, status='new')

do i=0,37200
read(16,*) xcoord2, ycoord2,x2,y2,mag2,merr2,sharp2,chi2
do j=0,37000
read(15,*) xcoord1(j), ycoord1(j),x1,y1,mag1,merr1,sharp1,chi1

if(ABS(xcoord1(j)-xcoord2).le.arcsec.and.ABS(ycoord1(i)-ycoor&
&d2).le.arcsec) then

write(17,*) xcoord1(j),ycoord1(j),x1,y1,mag1,merr1,&
&sharp1,chi1,xcoord2,ycoord2,x2,y2,&
&mag2,merr2,sharp2,chi2
 
Technology news on Phys.org
  • #2
You have not declared any arrays, yet, you are using indices with xcoord1 and ycoord1.

I don't see the 'end do' lines nor the file rewind...from what I can see, once you go thru the inner loop once (and you get to the end of that file), you will not be able to do that again without a rewind on that file.

I think it is best to simply read the entire array of data, at least, the one that is going to be in the inner loop; in the outer loop, you can get away by just reading one line at a time until the end of file.

So declare arrays for your xxxx1 variables long enough to read the entire file; then, go ahead and do the nested do loop...there will only be need to read a line in the outer loop...in the inner one, you can skip the read statement and go straight to the comparison (using array indices).
 
  • #3
Thank you so much, gsal, for taking the timel! I will try that today!
 

FAQ: Comparing line in one file to multiple lines in another/FORTRAN

How can I compare a single line in one file to multiple lines in another using FORTRAN?

To compare a single line in one file to multiple lines in another using FORTRAN, you can use a nested DO loop. The outer loop will iterate through each line in the first file, while the inner loop will compare that line to each line in the second file. You can use IF statements within the inner loop to check for matches and perform any necessary actions.

What is the benefit of using FORTRAN for line comparison?

FORTRAN is a powerful programming language that is optimized for scientific and mathematical calculations. It has built-in functions and features that make it efficient for handling large amounts of data, such as in comparing lines in files. Additionally, FORTRAN is a widely-used language in the scientific community, so there is a wealth of resources and support available for using it.

Can FORTRAN handle comparing lines from files with different formats?

Yes, FORTRAN is a versatile language that can handle various data formats. You can use built-in functions like READ and WRITE to read in lines from files with different formats and then compare them using string manipulation functions or logical operators.

Are there any specific techniques or methods for efficient line comparison in FORTRAN?

One useful technique for efficient line comparison in FORTRAN is to use arrays. You can read in lines from files and store them in arrays, which can then be easily compared using built-in array functions. This can also save memory and processing time compared to comparing lines directly from the files.

Is there a limit to the number of lines that can be compared using FORTRAN?

The limit on the number of lines that can be compared using FORTRAN will depend on the resources available on your computer, such as memory and processing power. However, FORTRAN is designed to handle large amounts of data, so it should be able to handle a significant number of lines without any issues.

Similar threads

Replies
16
Views
3K
Replies
5
Views
4K
Replies
5
Views
1K
Replies
1
Views
1K
Replies
1
Views
3K
Replies
2
Views
1K
Replies
5
Views
6K
Replies
3
Views
2K
Replies
5
Views
4K
Back
Top