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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

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