Print

Print


Here is a complete program that is a possible solution to a problem from our
next book

 

It involves breaking down some text into words, and counting the frequency
of occurrence

based on the length of each word. 

 

d:\document\fortran\newbook\examples\ch14>type ch1424.f90

program ch1423

implicit none

character (1024) :: string01

character (128)  :: t

character (128) , dimension(0:11) :: words

integer , dimension(0:11) :: nwords=0

character (23)    :: set='., '

integer :: i

integer :: l

integer :: start,startw,end,endw

  string01 = "The important issue about a language, is not so"

  string01  = trim(string01) // " " // "much what features the language
possesse

s, but"

  string01  = trim(string01) // " " // "the features it does possess, are
suffic

ient, to"

  string01  = trim(string01) // " " // "support the desired programming
styles,

in the"

  string01  = trim(string01) // " " // "desired application areas."

  l = len(trim(string01))

  start=1

  startw=1

  end=l

  endw=0

  do i=0,11

    words(i)=''

  end do

  t=''

  do

    i=scan(string01(start:end),set)

    if (i==0) exit

    start=start+i

    endw=start-2

    t=string01(startw:endw)

    l=len_trim(t)

    words(l)=trim(words(l)) // ' ' // t

    nwords(l)=nwords(l)+1

    t=''

    startw=start

  end do

  do i=1,11

    print *,i,' ',trim(words(i)),' ',nwords(i)

  end do

end program ch1423

d:\document\fortran\newbook\examples\ch14>nagfor ch1424.f90

NAG Fortran Compiler: Release 5.2(722)

[NAG Fortran Compiler normal termination]

 

d:\document\fortran\newbook\examples\ch14>a.exe

 1   a  1

 2   is so it to in  5

 3   The not the but the are the the  8

 4   much what does  3

 5   issue about areas  3

 6   styles  1

 7   possess support desired desired  4

 8   language features language features  4

 9   important possesses  2

 10   sufficient  1

 11   programming application  2

 

d:\document\fortran\newbook\examples\ch14>

 

  _____  

From: Fortran 90 List [mailto:[log in to unmask]] On Behalf Of
Ian Chivers
Sent: 16 March 2011 17:27
To: [log in to unmask]
Subject: Re: data parsing with F90

 

How about the scan function?

 

scan(string,set)

 

where set is the set of characters defined as delimiters.

 

Ian Chivers

 

  _____  

From: Fortran 90 List [mailto:[log in to unmask]] On Behalf Of
bennie blackwell
Sent: 16 March 2011 15:28
To: [log in to unmask]
Subject: data parsing with F90

 

I have a need to parse a character string of chemical species to 1)
determine the number of species and then 2) read the individual species as
character variables. An example might be the space delimited record for the
following 17 species:

 

N2 C O O2 CO2 CO N NO NO2 CN C3 C2 C2N2 C5 C4 C4N2 C20

 

I have an old f77 program (parse.f) written by a colleague in 1988 and
modified by myself in 1991 that allows a variety of delimiters, including
space, comma, <, =, and >. I could run parse.f through a F77 to F90
converter and move forward. However, I am looking for a more modern F90
approach. Is there a better approach than parsing each character
individually, checking to see if the single character matches the allowable
delimiter characters and mucking around in general.

 

Thanks,

 

Ben Blackwell

Blackwell Consulting

PO Box 2879

Corrales, NM 87048

505-897-5090

(fax) 505-890-4992

[log in to unmask]