Fortran: separate acids from a list of chemicals in a text file

  • Fortran
  • Thread starter carltonlacey
  • Start date
  • #1

Main Question or Discussion Point

Dear Fellow Members,

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.The letters ws and

we in the code stand for word start and word end. Can some

body please tell me where I am going wrong?

carltonlacey.
Code:
        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
 
Last edited by a moderator:

Answers and Replies

  • #2
33,287
4,990
I would change the approach to this:
Scan each line for the string "acid".
If found, store the portion of the string from the beginning through "acid" in a temporary string variable.
1. Pass that string to a subroutine that scans it for the string "ic".
2. If found, scan backwards from the position of the 'c' in "ic" to the first space.
3. Store that word, plus "acid" in a temporary string variable.
4. Write that string to your output file.

It might be that you are already doing something like that, but your program logic is difficult to follow, especially with no comments.
 

Related Threads on Fortran: separate acids from a list of chemicals in a text file

Replies
1
Views
2K
Replies
12
Views
13K
Replies
3
Views
19K
Replies
4
Views
10K
Replies
1
Views
16K
  • Last Post
Replies
13
Views
1K
Replies
1
Views
2K
Replies
1
Views
7K
Top