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

AI Thread Summary
The discussion revolves around a Fortran 95 program designed to extract acid names from a text file. The program aims to identify occurrences of the word "acid" and check if the preceding word contains "ic." However, it currently outputs entire sentences instead of just the acid names and misses some entries. The user is seeking help to identify errors in their code. Suggestions include revising the approach to first scan for "acid," then check the preceding word for "ic," and streamline the logic for better clarity. The need for clearer comments in the code is also highlighted to aid understanding and debugging.
carltonlacey
Messages
1
Reaction score
0
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:
Technology news on Phys.org
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
6
Views
2K
Replies
33
Views
5K
Replies
3
Views
3K
Replies
2
Views
2K
Replies
3
Views
4K
Replies
12
Views
15K
Replies
2
Views
2K
Replies
10
Views
2K
Replies
16
Views
2K
Back
Top