carltonlacey
Technical User
Dear Fellow Members, 28/10/2011
I am presently trying to write a Fortran 95 program to
separate acids from a list of chemicals in a text file and
then write them to a second text file. The program first
matches the word "acid" and is then supposed to check
if the word preceding the word "acid" contains the letters
"ic" (For example Sulphuric Acid, Nitric Acid). If both
are true, then it should print the name of the acid on to
the second text file.
The problem is that it prints out the whole sentence
containing the name of the acid and also skips many other
acid names from the same paragraph. I am stuck as I can't
figure out where I have made errors in the code. I am
pasting below the code I have written. Can some body
please tell me where I am going wrong?
Thanks and regards
carltonlacey
Program Abs reader
c
character*80 line, dsn, tsave
character*1 reply
integer*4 i, we2, ws2, we1, ws1
logical chem
c
write(*,'(a,$)') 'Input file name:'
read(*,'(a)',end=21)dsn
open(unit=10,file=dsn,status='old')
open(unit=30,file='abscopy.txt',status='unknown')
c
12 read(10,'(a)',end=21)line
do k = 1,80
if (line(k:k).ne.' ')then
ws2=k
end if
do i=k+1,80
if(line(i:i).eq.' ')then
we2= i-1
chem = .false.
call chemical2(ws2,we2,chem,line)
if (chem) then
tsave=line(ws2:we2)
end if
we1=ws2-2
do j=we1,1
if(line(j:j).eq.' ')then
ws1=j+1
call chemical1(ws1,we1,tsave,line,ws2,we2,chem)
if (chem) then
write(30,'(a)')line(ws1:we1),tsave(ws2:we2)
c write(30,'(a)')
ws2=we2+2
goto 13
end if
end if
end do
end if
13 continue
end do
goto 12
end do
21 continue
stop
end
c This subroutine tells the program to record words that fit certain criteria.
c
subroutine chemical2(ws2,we2,chem,line)
character*80 line, tsave
integer*4 ws2, we2
logical chem
c
c check for 'acid' and record
do n=ws2,we2
if(line(n:n+3).eq.'acid')then
chem=.true.
return
end if
end do
return
end
c
c
subroutine chemical1(ws1,we1,tsave,line,ws2,we2,chem)
character*80 line,tsave
integer*4 ws1, we1, ws2, we2
logical chem
c
c check previous word has 'ic'
if(line(we1-1:we1).eq.'ic')then
chem=.true.
return
end if
return
end
I am presently trying to write a Fortran 95 program to
separate acids from a list of chemicals in a text file and
then write them to a second text file. The program first
matches the word "acid" and is then supposed to check
if the word preceding the word "acid" contains the letters
"ic" (For example Sulphuric Acid, Nitric Acid). If both
are true, then it should print the name of the acid on to
the second text file.
The problem is that it prints out the whole sentence
containing the name of the acid and also skips many other
acid names from the same paragraph. I am stuck as I can't
figure out where I have made errors in the code. I am
pasting below the code I have written. Can some body
please tell me where I am going wrong?
Thanks and regards
carltonlacey
Program Abs reader
c
character*80 line, dsn, tsave
character*1 reply
integer*4 i, we2, ws2, we1, ws1
logical chem
c
write(*,'(a,$)') 'Input file name:'
read(*,'(a)',end=21)dsn
open(unit=10,file=dsn,status='old')
open(unit=30,file='abscopy.txt',status='unknown')
c
12 read(10,'(a)',end=21)line
do k = 1,80
if (line(k:k).ne.' ')then
ws2=k
end if
do i=k+1,80
if(line(i:i).eq.' ')then
we2= i-1
chem = .false.
call chemical2(ws2,we2,chem,line)
if (chem) then
tsave=line(ws2:we2)
end if
we1=ws2-2
do j=we1,1
if(line(j:j).eq.' ')then
ws1=j+1
call chemical1(ws1,we1,tsave,line,ws2,we2,chem)
if (chem) then
write(30,'(a)')line(ws1:we1),tsave(ws2:we2)
c write(30,'(a)')
ws2=we2+2
goto 13
end if
end if
end do
end if
13 continue
end do
goto 12
end do
21 continue
stop
end
c This subroutine tells the program to record words that fit certain criteria.
c
subroutine chemical2(ws2,we2,chem,line)
character*80 line, tsave
integer*4 ws2, we2
logical chem
c
c check for 'acid' and record
do n=ws2,we2
if(line(n:n+3).eq.'acid')then
chem=.true.
return
end if
end do
return
end
c
c
subroutine chemical1(ws1,we1,tsave,line,ws2,we2,chem)
character*80 line,tsave
integer*4 ws1, we1, ws2, we2
logical chem
c
c check previous word has 'ic'
if(line(we1-1:we1).eq.'ic')then
chem=.true.
return
end if
return
end