From: Jim Xia on


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

Yes, listed-directed (funny I also called them free formatted many
years ago without knowing the right term) read can do that job
provided following conditions are met
1.) you know the data type of the variables you're reading. This is
important as string "1" can be interpreted as either a real data or an
integer data. String "T" can be treated as string or logical values.
So knowing the data type you're reading in is required.
2.) you know the exact number of variables you're reading.
3.) all strings are enclosed by appropriate delimiters. The safest
delimiter in input files are quotes (" ") and single quotes (' '), you
can have spaces within a quoted string. Spaces or white spaces belong
to the next group of delimiters that after quotes.

You should also keep in mind the end of line ("\n") is treated just as
another value separator. And if you specify more list items on the
read statement than there are in the input file, the read statement
will attempt to read the next line. So knowing details about the
input data and list items are important in using listed-directed IO.

Cheers,

Jim
From: jfh on
On Apr 30, 9:09 am, ivan martin <i.mar...(a)invalid.com> wrote:
> 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

It can be done but how you do it depends on the answers to these 8
questions: 1. Are the numbers allowed to contain e E d or D? After
all, 3e3 is a valid real number. 2. May the string contain digits e.g.
ZZr4? 3. May the string contain letters e E d or D? 4. Is there always
a blank space between the last number and the string? 5. May there be
more than one blank between numbers? 6. Must input numbers without
decimal points or e or d be treated as integer? 7. Can you specify in
advance how long the longest input line may be? 8. Does every line
contain the same number of numbers or may they differ?

The basic idea is to read each line into a character variable with A
format, and then read each number in the line in turn with * format,
but only you can decide what inputs are erroneous and what to do if
they do.

-- John Harper


From: glen herrmannsfeldt on
Jim Xia <jimxia(a)hotmail.com> wrote:
(snip)

> Yes, listed-directed (funny I also called them free formatted many
> years ago without knowing the right term) read can do that job
> provided following conditions are met

It may have had other names before it was added to the standard.

PL/I has the LIST keyword, so list directed. (Along with EDIT
directed and DATA directed I/O.)

> 1.) you know the data type of the variables you're reading. This is
> important as string "1" can be interpreted as either a real data or an
> integer data. String "T" can be treated as string or logical values.
> So knowing the data type you're reading in is required.
> 2.) you know the exact number of variables you're reading.
> 3.) all strings are enclosed by appropriate delimiters. The safest
> delimiter in input files are quotes (" ") and single quotes (' '), you
> can have spaces within a quoted string. Spaces or white spaces belong
> to the next group of delimiters that after quotes.

-- glen
From: Terence on
On Apr 30, 1:17 pm, glen herrmannsfeldt <g...(a)ugcs.caltech.edu> wrote:
> Jim Xia <jim...(a)hotmail.com> wrote:
>
> (snip)
>
> > Yes, listed-directed (funny I also called them free formatted many
> > years ago without knowing the right term) read can do that job
> > provided following conditions are met
>
> It may have had other names before it was added to the standard.
>
> PL/I has the LIST keyword, so list directed.  (Along with EDIT
> directed and DATA directed I/O.)
>
> > 1.) you know the data type of the variables you're reading.  This is
> > important as string "1" can be interpreted as either a real data or an
> > integer data.  String "T" can be treated as string or logical values.
> > So knowing the data type you're reading in is required.
> > 2.) you know the exact number of variables you're reading.
> > 3.) all strings are enclosed by appropriate delimiters.  The safest
> > delimiter in input files are quotes (" ") and single quotes (' '), you
> > can have spaces within a quoted string.  Spaces or white spaces belong
> > to the next group of delimiters that after quotes.
>
> -- glen

Good advice above. However, by first reading each line as a character
string, and then applying list directed input from the string, you
will avoid the nasty problem of what happens when you have diffetent
numbers of values on the individual lines, in which case a formatted
may try to read the next line to satisfhthe number of input variable.

From: robin on
"glen herrmannsfeldt" <gah(a)ugcs.caltech.edu> wrote in message news:hrdi3g$i33$1(a)naig.caltech.edu...
| Jim Xia <jimxia(a)hotmail.com> wrote:
| (snip)
|
| > Yes, listed-directed (funny I also called them free formatted many
| > years ago without knowing the right term) read can do that job
| > provided following conditions are met
|
| It may have had other names before it was added to the standard.
|
| PL/I has the LIST keyword, so list directed. (Along with EDIT
| directed and DATA directed I/O.)

And in PL/I, all those three forms can be used on the same file,
interchangebly, if required, to great advantage.


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