Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fortran 'read' statement question

0 views
Skip to first unread message

jj_76

unread,
Jun 17, 2008, 1:29:19 PM6/17/08
to
hi,

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

dpb

unread,
Jun 17, 2008, 1:55:56 PM6/17/08
to
jj_76 wrote:
...snip example of attempting list-directed read on data file w/ "/"
character in record...

...

"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.

--


Richard Maine

unread,
Jun 17, 2008, 2:09:34 PM6/17/08
to
dpb <no...@non.net> wrote:

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

jj_76

unread,
Jun 17, 2008, 2:41:28 PM6/17/08
to

Thanks...FORMAT statement worked !

James Giles

unread,
Jun 17, 2008, 2:50:09 PM6/17/08
to
Richard Maine wrote:
> dpb <no...@non.net> wrote:
>
>> jj_76 wrote:
>> ...snip example of attempting list-directed read on data file w/ "/"
>> character in record...
...
> 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).

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


Richard Maine

unread,
Jun 17, 2008, 3:10:00 PM6/17/08
to
James Giles <james...@worldnet.att.net> wrote:

> 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.

jj_76

unread,
Jun 17, 2008, 3:16:26 PM6/17/08
to

>
> 1. Read the whole line into a character string using "A" format.
--------------

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.

dpb

unread,
Jun 17, 2008, 3:40:12 PM6/17/08
to

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.

--

relaxmike

unread,
Jun 18, 2008, 9:46:00 AM6/18/08
to
Hi,

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

robin

unread,
Jun 28, 2008, 12:01:42 AM6/28/08
to
"Richard Maine" <nos...@see.signature> wrote in message
news:1iiohra.17ipynwti92b0N%nos...@see.signature...

> James Giles <james...@worldnet.att.net> wrote:
>
> > 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.

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.


0 new messages