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

In summary: You could also try using the INDEX function instead of the loop to find the "acid" and "ic" strings. In summary, the program is trying to separate acids from a list of chemicals in a text file and write them to a second text file. However, the current code is not functioning correctly and is skipping some acid names. The suggested approach is to scan each line for "acid" and then check if the previous word contains "ic". If both criteria are met, the acid name is written to the output file. Using the INDEX function and clearer program logic could help improve the code.
  • #1
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
  • #2
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.
 

1) How does Fortran separate acids from a list of chemicals in a text file?

Fortran uses a combination of string manipulation and logical statements to identify the acids in the list of chemicals. It checks for specific keywords and patterns in the chemical names, such as "acid" or "HCl", and then separates those chemicals into a separate list or file.

2) Can Fortran handle different file formats for the list of chemicals?

Yes, Fortran can be programmed to handle various file formats, such as .txt, .csv, or .dat. It is important to specify the correct file format in the code so that Fortran can properly read and manipulate the data.

3) Is Fortran efficient in separating a large number of chemicals?

Yes, Fortran is designed to handle large amounts of data efficiently. It uses optimized algorithms and can handle complex logical operations, making it ideal for tasks such as separating a large list of chemicals.

4) Are there any limitations to using Fortran for this task?

Fortran may have some limitations when it comes to handling non-standard or uncommon chemical names. It relies on specific keywords and patterns, so if the chemical names do not follow these conventions, the results may not be accurate. Additionally, Fortran may not be the best choice for tasks that require real-time processing of data.

5) How can I learn to use Fortran for this task?

There are many online resources, tutorials, and courses available for learning Fortran. It is recommended to have a basic understanding of programming concepts and syntax before diving into Fortran. Additionally, reading the documentation and working on practice problems can help improve your skills in using Fortran for separating acids from a list of chemicals in a text file.

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

Replies
5
Views
1K
Replies
3
Views
1K
Replies
2
Views
828
Replies
5
Views
775
Replies
10
Views
1K
Replies
12
Views
2K
Back
Top