Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Oct 19, 2013 #1
    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

    &d2).le.arcsec) then

    write(17,*) xcoord1(j),ycoord1(j),x1,y1,mag1,merr1,&
  2. jcsd
  3. Oct 19, 2013 #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).
  4. Oct 20, 2013 #3
    Thank you so much, gsal, for taking the timel! I will try that today!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook