From: RLFitch on
In a write statement that contains an implied do loop such as:

print *, (k, k = 1, numk)

is it possible to add a format that has a variable: write (*, '(x,
numk(I3))')

R Fitch
From: Gordon Sande on
On 2010-01-27 14:01:24 -0400, RLFitch <rlfitch(a)gmail.com> said:

> In a write statement that contains an implied do loop such as:
>
> print *, (k, k = 1, numk)
>
> is it possible to add a format that has a variable: write (*, '(x,
> numk(I3))')
>
> R Fitch

You have a "problem" but you do not say what is "wrong" or what you hoped for.
You did give a (guessed at) "solution". Now we have to reverse engineer your
non-solution to figure out both your problem and your desired result. But mind
reading is not very effectuve in the presence of lots of noise.

Do you want each output number to have a differing width? Or a fixed width?
Across the page? Down the page? Labelled by an index? Etc....?

Fortran has a very powerful output mechanism. Formatting is a small language
by itself and can easily do things that are awkward in other languages. But
one has to know what is intended.

Many folks who ask for help forget to supply even the most basic of contexts
and suggest solutions that are often wide of the mark. The basic fact is that
if you asking then you solution is not likely to be a success and you may not
even know what the cause is. This is usually a line of code with a problem
in the lines not show.

So show us what is "wrong" and why you think it is wrong. Suggest what would be
better.

Try something like

write ( 6, '(99i3)' ) (k, k=1,numk)

to get (up to 99) width three numbers across the page.



From: Richard Maine on
RLFitch <rlfitch(a)gmail.com> wrote:

> In a write statement that contains an implied do loop such as:
>
> print *, (k, k = 1, numk)
>
> is it possible to add a format that has a variable: write (*, '(x,
> numk(I3))')

Not in any way close to that, but there are plenty of other ways to get
the job done. There is a nonstandard feature of some compilers that is
somewhat like that, but it is nonportable, has "issues", and I strongly
recommend against using it.

For the large majority of cases, I recommend a very simple solution. Use
something like 999I3, where the 999 is any number sufficiently large
that you can be sure that numk will not exceed it. Some people are
unaware that there is no problem at all in having the format specify
more data fields than get used. I have seen people code a mess of more
than an entire page (back when one usually looked at code on paper
printouts) of IF statements to select one of a dozen or so cases just
because they didn't realize this.

The simple form won't work well if you have a more complicated format -
for example if you have some more after the variable length part. It
also won't work well if there is truly no finite number that can be used
as the upper bound (but that is unusual).

If you have a case where the simple solution doesn't apply, then you can
always generate a format using an internal write into a character
string, as in

character :: fmt*32
...
write (fmt,*) '(1x,' , numk, 'i3)'
write (*,fmt) (k,k=1,numk)

Variants of this are extremely flexible and can do almost anything you
might desire with formating. But I must reemphasize that it is usually
not needed. Stick with the simpler form in most cases.

Another alternative involves non-adancing I/O. Yet another involves
writing the data internally to a temporary character string. There are
probably others that I'm not thinking of. But the simple approach I
mentioned first is what you probably want.

P.S. Unrelated to the part you asked about, but the x in the format in
your example WRITE statement is nonstandard. Some compilers will accept
it as an extension, but the standard form of the X edit descriptor
requires a field width; it shoud be 1X instead of just X.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: RLFitch on
>
> For the large majority of cases, I recommend a very simple solution. Use
> something like 999I3, where the 999 is any number sufficiently large
> that you can be sure that numk will not exceed it. Some people are
> unaware that there is no problem at all in having the format specify
> more data fields than get used. I have seen people code a mess of more
> --
> Richard Maine                    | Good judgment comes from experience;
> email: last name at domain . net | experience comes from bad judgment.
> domain: summertriangle           |  -- Mark Twain
>
>
>...
>
> P.S. Unrelated to the part you asked about, but the x in the format in
> your example WRITE statement is nonstandard. Some compilers will accept
> it as an extension, but the standard form of the X edit descriptor
> requires a field width; it shoud be 1X instead of just X.
>
> So show us what is "wrong" and why you think it is wrong. Suggest what would be
> better.
> Try something like
>
> write ( 6, '(99i3)' ) (k, k=1,numk)
>
> to get (up to 99) width three numbers across the page.

Perfect! I was one of few unaware of using 999I3 and not realizing (of
course) that I use only what's needed. Simple enough for me and yet
very effective!

As to 'x' versus '1X'. It's amazing how pliant some compilers can be.
I'm sure that my coding gets messier as I find what the compiler
accepts and what causes a compile time error, usually by accident. I'm
using Compaq FORTRAN on OpenVMS.

thanks,
Ransom Fitch
From: robin on
"RLFitch" <rlfitch(a)gmail.com> wrote in message news:c0b0096c-2167-4165-a16c-65c1b24c7ca5(a)m16g2000yqc.googlegroups.com...
| In a write statement that contains an implied do loop such as:
|
| print *, (k, k = 1, numk)
|
| is it possible to add a format that has a variable: write (*, '(x,
| numk(I3))')

In PL/I you can, but not in Fortran.

You can, however, create a format at run time,
but simplest is just to write 1x, 1(I3)
or 1x, 100(I3)