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!

help

Status
Not open for further replies.

acharls

Technical User
May 20, 2013
9
IN
Hi all,

I have two files
111
112
113
111
114
&
115
115
116
117
117
118

I wanted to compare each value from file 1 with all values in file 2 and write the outputs if file1(i)+3==file2. my code is

open(11,file='f1')
i=1
32 read(11,*,end=98)file1(i)
open (12,file='f2')
31 read(12,*,end=99)file2
if(file1(i)+3.eq.file2)then
write(*,*)file1(i),file2,i
end if
go to 31
99 continue
close(12)
i=i+1
go to 32
98 continue
end program test

output is

112 115 2
112 115 2
113 116 3
114 117 5
114 117 5

the third col is array index I am looking how this array index can be made continuous ie 1,2,3 instead 2,3,5

Thanks in advance
 
Like this ?
Code:
112 115 1
112 115 2
113 116 3
114 117 4
114 117 5
 
Actually it should be

1 instead 2
1 2
2 3
3 5
3 5

Thanks

Charls Antony
 
Then you have to store the value of previous file2 in file2_old and then increment the index only when file .ne. file2_old

I tried it a do got
Code:
$ acharls
         112         115           1
         112         115           1
         113         116           2
         114         117           3
         114         117           3
 
Please do not use 'go-to', there still is a place for 'go-to', but this is not one of them. Please use a do-loop.

Second, please do not keep opening and closing files inside a loop, this is totally unnecessary; simply read all the data into the arrays ahead of the loops and that is it.

The reason why you currently have a repeated "index" is because your 'i' currently represent the index of the iterm from file 1...and because such item finds two matches from file 2, well, it prints the same number twice.
 
Hi,

Thanks mikrom & salgerman. I solved it.

Charls


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top