I have the following data format:
Date: 1997 2 21.0 dE/E: 1.34138E-07 dL/L: 2.61358E-15
Date: 1998 7 6.0 dE/E: 8.72011E-08 dL/L: 5.22716E-15
Date: 1999 11 18.0 dE/E: 1.25871E-07 dL/L: 3.92037E-15
Date: 2001 4 1.0 dE/E: 2.85034E-08 dL/L: 4.29374E-15
Date: 2002 8 14.0 dE/E: 1.42615E-07 dL/L: 1.86684E-15
When I try to read this data using the following 'read' statement, it
is not reading beyond 'dE'. It does not print anything beyond the 'dE'
in the above file (i.e, starting from '/')....My 'read' statement and
output is below:
read(10,*)test1,test2,test3,test4,test5,test6
print *,test1,test2,test3,test4,test5,test6
output:
Date: 1997 2 21.0 dE
Date: 1998 7 6.0 dE
Date: 1999 11 18.0 dE
Date: 2001 4 1.0 dE
Date: 2002 8 14.0 dE
Any help is appreciated
Thanks
...
"If a slash ( / ) is encountered during execution, the READ statement is
terminated, and any remaining input list items are unchanged."
You need an explicit FORMAT statement.
--
Yes. Though that's a slight PITA for data like that shown, which appears
to be blank delimitted instead of in fixed field widths.
My recommended approach for data like this (assuming that doing
something like changing the input data format isn't an option - quotes
would solve it easily for example) is:
1. Read the whole line into a character string using "A" format.
2. Separate the fields by searching for the blank delimitters. Make use
of the INDEX, SCAN, and/or VERIFY intrinsics to do so. I've got my own
subroutine that I use for such things, but it in turn uses other of my
utility procedures. Probably about as easy to roll your own; it isn't
very hard. Most people that do a lot of Fortran input parsing probably
have one in their toolbox of handy routines.
3. Use an internal list-directed read to decode the data from each field
(except for the character fields, which don't need any further
decoding).
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Thanks...FORMAT statement worked !
In this case, why not use your step 1, but then replace all the '/'
characters with something else ('÷' say)? Then read the whole
buffer with internal list-directed. To be sure, there also isn't
a REPLACE intrinsic. But, as you say, it's fairly easy to roll your
own. No sense reproducing the kind of parsing list-directed
already does.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
"Simplicity is prerequisite for reliability" -- E. W. Dijkstra
> In this case, why not use your step 1, but then replace all the '/'
> characters with something else ('÷' say)? Then read the whole
> buffer with internal list-directed.
That would also work.
That's exactly what I did. I read the whole thing into a character
string and converted the required numbers into floats:
character(len=67)::test1
real *8 frac
read(10,100)test1
read(test1(36:46),fmt='(f11.9)')frac
100 format(a67)
thanks for the help.
If that was all that was needed, the following would be somewhat easier
ways (for the future)...
read(10,'(A)') test1
read(test1(36:46),*) frac
a) The simple "A" format will read the length of the variable w/o a
precise count, and
b( The substring selection eliminates the "/" problem character so you
can use list-directed formatting again.
Or, since it was a fixed-column file, you could simply skip the required
number of columns something like
read(10,'(35X,E11.5)') frac
Or, as Richard and James pointed out, a little alternative work in one
of a couple of ways could leave you w/ being able to use the
list-directed i/o, too.
--
In that particular situation, I would personally not try to
find a suitable fortran format to read the data.
That way of doing imply that each file requires its own parser,
which is at the exact opposite of current software practices.
Instead, I would turn the file into a standard data format,
and directly use a component to read that format.
For example, it would be easy to transform the previous data into
a csv data file and use a csv fortran component, isn'it ?
Regards,
Michaël
Not if the line already has the character '÷' in it.
However, this difficulty can be overcome by
keeping a record of which characters were replaced,
and then later re-instating the altered ones.