Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading words with varying length from input file

Status
Not open for further replies.

CCarl

Programmer
Sep 24, 2009
2
BE
Hi,

I need to write a program that can read words from an input file. These words need to be stored in a vector "character species(200,20)". The max number of words is 200, the max length is 20 of each word is 20.

The words can be on the same line (separated by blanks) or in different lines or a combination of both, eg;

h2o c2h2 c2h4
h2 h
ch4
ch3 ch2 c2h3
 
This is the code i've writte so far;

999 do while (mol/='END') cEND in the file indicates the last word is written


888 read(1,'(A)', iostat = io, advance='no'), ch

if (io ==0) then
if(ch == BL .and. oldch /= BL)
& then c new species found

le(nspecies) = lengte
nspecies = nspecies + 1
lengtemol=1
lengte=0
! print*, 'new species found'
! print*, species(nspecies,1:20)
mol=''
n=1
oldch = ch
goto 888

else if (index(LETTER,ch) /=0) then
c reading further in same species
!print*, 'reading continues'
mol=mol(1:lengte)//ch
print*, mol
species(nspecies,lengtemol)=ch
n=1
lengtemol=lengtemol+1
lengte=lengte+1
oldch = ch
goto 999

end if
oldch = ch
end if
end do

It works for words on the same line but as soon a new line begins, the last word of the previous line and the first word of the new line are taken together as 1 word.

Any ideas?
 
For the given datafile
species.dat
Code:
h2o c2h2 c2h4
  h2 h
ch4
ch3 ch2 c2h3
this program does what you want to do
species.f95
Code:
[COLOR=#a020f0]program[/color] species
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: n [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]200[/color]
[COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]20[/color]) :: fname, line, species_array(n) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]''[/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: stat, nr_lines, nr_words

fname [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'species.dat'[/color]
[COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname, [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color], [COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
[COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
  [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]
[COLOR=#804040][b]end if[/b][/color]

[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Processing file...'[/color]
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
nr_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]  [COLOR=#0000ff]! number of lines in the file[/color]
nr_words [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]  [COLOR=#0000ff]! number of words found[/color]
[COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color]([COLOR=#ff00ff].true.[/color])
  nr_lines [COLOR=#804040][b]=[/b][/color] nr_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color]) line
  [COLOR=#0000ff]! search for words in the line and store them into the array[/color]
  [COLOR=#a020f0]call[/color] process_line( [COLOR=#008080]trim[/color](line), nr_lines, nr_words, species_array, n)
[COLOR=#804040][b]enddo[/b][/color]

[COLOR=#6a5acd]99[/color] [COLOR=#804040][b]continue[/b][/color]
[COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Printing array...'[/color]
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#a020f0]call[/color] print_array([COLOR=#ff00ff]'Word'[/color], species_array, nr_words)
[COLOR=#a020f0]end program[/color] species

[COLOR=#a020f0]subroutine[/color] process_line(line, i, k, array, n)
  [COLOR=#0000ff]! line  = input line[/color]
  [COLOR=#0000ff]! i     = line index[/color]
  [COLOR=#0000ff]! k     = word index index in the words array[/color]
  [COLOR=#0000ff]! array =  array of words[/color]
  [COLOR=#0000ff]! n     = dimension of the words array[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: line
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: i, n 
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color] [COLOR=#2e8b57][b]out[/b][/color]) :: k
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]dimension[/b][/color](n), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]out[/b][/color]) :: array 
  [COLOR=#0000ff]! temp vars[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]20[/color]) :: word
  [COLOR=#2e8b57][b]integer[/b][/color] :: j, beg_idx, end_idx

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) i, [COLOR=#ff00ff]".line: '"[/color], line, [COLOR=#ff00ff]"'"[/color]
  beg_idx [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#0000ff]! begin index of the word[/color]
  end_idx [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#0000ff]! end index of the word[/color]
  j [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color]       [COLOR=#0000ff]! character position in the line[/color]
  [COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] (j [COLOR=#804040][b]<=[/b][/color] [COLOR=#008080]len[/color](line))
    [COLOR=#804040][b]if[/b][/color] ((beg_idx [COLOR=#804040][b].eq.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b].and.[/b][/color] (line(j:j) [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]' '[/color])) [COLOR=#804040][b]then[/b][/color]
       beg_idx [COLOR=#804040][b]=[/b][/color] j
       [COLOR=#0000ff]!write (*,*) 'beg_idx = ', beg_idx       [/color]
    [COLOR=#804040][b]end if[/b][/color]
    [COLOR=#804040][b]if[/b][/color] (beg_idx [COLOR=#804040][b].gt.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]if[/b][/color] (line(j:j) [COLOR=#804040][b].eq.[/b][/color] [COLOR=#ff00ff]' '[/color]) [COLOR=#804040][b]then[/b][/color]
        end_idx [COLOR=#804040][b]=[/b][/color] j [COLOR=#804040][b]-[/b][/color] [COLOR=#ff00ff]1[/color]
      [COLOR=#804040][b]end if[/b][/color]
      [COLOR=#804040][b]if[/b][/color] (j [COLOR=#804040][b].eq.[/b][/color] [COLOR=#008080]len[/color](line)) [COLOR=#804040][b]then[/b][/color] 
        end_idx [COLOR=#804040][b]=[/b][/color] j
      [COLOR=#804040][b]end if[/b][/color]
      [COLOR=#804040][b]if[/b][/color] (end_idx [COLOR=#804040][b].gt.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
         [COLOR=#0000ff]!write(*,*) 'end_idx = ', end_idx[/color]
         k [COLOR=#804040][b]=[/b][/color] k [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
         word [COLOR=#804040][b]=[/b][/color] line(beg_idx:end_idx)
         [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'  '[/color], k,[COLOR=#ff00ff]".word = '"[/color], [COLOR=#008080]trim[/color](word),[COLOR=#ff00ff]"'"[/color]
         array(k) [COLOR=#804040][b]=[/b][/color] word
         [COLOR=#0000ff]! initialize indices[/color]
         beg_idx [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
         end_idx [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
      [COLOR=#804040][b]end if[/b][/color]
    [COLOR=#804040][b]end if[/b][/color]
    j [COLOR=#804040][b]=[/b][/color] j [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]  
  [COLOR=#804040][b]end do[/b][/color]     
[COLOR=#a020f0]end subroutine[/color] process_line

[COLOR=#a020f0]subroutine[/color] print_array(array_name, array, n)
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: array_name 
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]dimension[/b][/color](n), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: array
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: n
  [COLOR=#0000ff]! temp vars[/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: j
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]3[/color]) :: j_string
  [COLOR=#804040][b]do[/b][/color] j [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n
    [COLOR=#804040][b]write[/b][/color] (j_string, [COLOR=#ff00ff]'(i3.3)'[/color]) j
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) array_name, [COLOR=#ff00ff]'['[/color], j_string, [COLOR=#ff00ff]'] = '[/color], array(j)
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end subroutine[/color] print_array
Output:
Code:
$ g95 species.f95 -o species

$ species

 Processing file...

 1 .line: 'h2o c2h2 c2h4'
    1 .word = 'h2o'
    2 .word = 'c2h2'
    3 .word = 'c2h4'
 2 .line: '  h2 h'
    4 .word = 'h2'
    5 .word = 'h'
 3 .line: 'ch4'
    6 .word = 'ch4'
 4 .line: 'ch3 ch2 c2h3'
    7 .word = 'ch3'
    8 .word = 'ch2'
    9 .word = 'c2h3'

 Printing array...

 Word[001] = h2o                 
 Word[002] = c2h2                
 Word[003] = c2h4                
 Word[004] = h2                  
 Word[005] = h                   
 Word[006] = ch4                 
 Word[007] = ch3                 
 Word[008] = ch2                 
 Word[009] = c2h3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top