From: ivan martin on
Hello everyone,

first, let me just send a big thanks to mr. Bowler, mr. Xia, and all the others
who helped me with my last question regarding reading input lines depending on the
first character.

If I may abuse your good will a little more ;)

A simple thing really, but I was wondering could it be made simpler.
I have in my file some input lines, which look like a mixture of numbers
and strings (no spaces, just one word, usually one letter):

1 14. 28. 2 5600. ZZTop

(made up example :), but I beliee the exact number of numbers isn't important for the
sake of this problem)

and I have a lot of those lines. Now, I've always read them by using predefined format
which specify the number of decimal places and ..., but I was wondering; could the format
be made in a way that the numbers are read in "free" format (*), and only the column where
the string starts and ends be specified, so as to make the writing of the input file easier
(in terms that one wouldn't have to be careful about where he is putting the numbers).

I'm generally trying to see what options do I have when I have numbers AND a string intermixed
in a line. Specifying a read format has always been something I've always disliked.

Best regards,
Ivan
From: Arjan on
> I'm generally trying to see what options do I have when I have numbers AND a string intermixed
> in a line. Specifying a read format has always been something I've always disliked.


Reading 1 line [CHARACTER(DefaultLength) :: ALine] at a time with
format '(A)' with
INTEGER, PARAMETER :: DefaultLength = 512 ! or higher
followed by post=processing of ALine is very flexible.
You don't have to re-read the line if you decide to do something else
than anticipated.

Arjan
From: ivan martin on
On Thu, 29 Apr 2010 15:42:02 -0700 (PDT), Arjan <arjan.van.dijk(a)rivm.nl> wrote:

>> I'm generally trying to see what options do I have when I have numbers AND a string intermixed
>> in a line. Specifying a read format has always been something I've always disliked.
>
>
>Reading 1 line [CHARACTER(DefaultLength) :: ALine] at a time with
>format '(A)' with
> INTEGER, PARAMETER :: DefaultLength = 512 ! or higher
>followed by post=processing of ALine is very flexible.
>You don't have to re-read the line if you decide to do something else
>than anticipated.
>
>Arjan


So, if I didn't get wrong the whole idea, without going into details, it is generally, for all these
kinds of manipulations,
best to read the whole line in a character variable, and then by means of internal read, "process"
it up?

Ivan
From: David Duffy on
ivan martin <i.martin(a)invalid.com> wrote:

> A simple thing really, but I was wondering could it be made simpler.
> I have in my file some input lines, which look like a mixture of numbers
> and strings (no spaces, just one word, usually one letter):

> 1 14. 28. 2 5600. ZZTop

> and I have a lot of those lines. Now, I've always read them by using predefined format
> which specify the number of decimal places and ..., but I was wondering; could the format
> be made in a way that the numbers are read in "free" format (*), and only the column where
> the string starts and ends be specified

> I'm generally trying to see what options do I have when I have numbers AND a string intermixed
> in a line. Specifying a read format has always been something I've always disliked.

One way is a scanner to extract each token on the line and then determine if
it is number or string. Flexible, but a little overhead. For example, Arjen Markus's
flibs has something suitable IIRC.

2c, David Duffy.

--
| David Duffy (MBBS PhD) ,-_|\
| email: davidD(a)qimr.edu.au ph: INT+61+7+3362-0217 fax: -0101 / *
| Epidemiology Unit, Queensland Institute of Medical Research \_,-._/
| 300 Herston Rd, Brisbane, Queensland 4029, Australia GPG 4D0B994A v
From: glen herrmannsfeldt on
ivan martin <i.martin(a)invalid.com> wrote:

(snip)
> A simple thing really, but I was wondering could it be made simpler.
> I have in my file some input lines, which look like a mixture of numbers
> and strings (no spaces, just one word, usually one letter):

> 1 14. 28. 2 5600. ZZTop

> (made up example :), but I beliee the exact number of numbers
> isn't important for the sake of this problem)

> and I have a lot of those lines. Now, I've always read them by
> using predefined format which specify the number of decimal
> places and ...,

Note that for numeric input with a decimal point specified,
the number after the decimal point in the F descriptor doesn't
matter at all. You do have to get the width right, such that
the input data is within the field width.

There is a small advantage in some cases to specifying the
number of digits after the decimal point, and not punching the
decimal onto the input cards. Yes, it made some sense for card
input, but even for cards most of the time one would specify
the decimal point. For terminal input it doesn't work well
at all, and only slightly better for reading from a disk file.

> but I was wondering; could the format be made in a way that the
> numbers are read in "free" format (*), and only the column where

For list directed (*) input:

If the next effective item is of type default, ASCII, or ISO 10646
character and
(1) The character sequence does not contain value separators,
(2) The character sequence does not cross a record boundary,
(3) The first nonblank character is not a quotation mark or an
apostrophe,
(4) The leading characters are not digits followed by an asterisk,
and
(5) The character sequence contains at least one character,


the delimiting apostrophes or quotation marks are not required.
If the delimiters are omitted, the character sequence is terminated
by the first blank, comma, slash, or end of record; in this case
apostrophes and quotation marks within the datum are not to be doubled.

> the string starts and ends be specified, so as to make the writing
> of the input file easier (in terms that one wouldn't have to be
> careful about where he is putting the numbers).

It seems that you could use list-directed input, but check those
conditions carefully.

> I'm generally trying to see what options do I have when I have
> numbers AND a string intermixed in a line. Specifying a read
> format has always been something I've always disliked.

Yes list-directed is somewhat easier. If you don't satisfy those
requirement, then you need apostrophes or quotes around the
character constant.

-- glen
 |  Next  |  Last
Pages: 1 2
Prev: ifort volatile with O3
Next: ASF bug (?)