Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
45 Views

*G0 data edit descriptor

Consider the following code, which outputs the content of an array in a string of the form [value,value,value]

PROGRAM P
IMPLICIT NONE
INTEGER,ALLOCATABLE :: A(:)
A = [1,2,3]
WRITE(*,FMT='("[",<SIZE(A)-1>(G0,","),G0,"]")') A
WRITE(*,FMT='("[",*(G0,","),"]")') A
WRITE(*,FMT='("[",3(G0,","),"]")') A
END PROGRAM

The output is:

[1,2,3]
[1,2,3,
[1,2,3,]

1. Although it is not standard-conformant (it's an Intel extension I believe), the first option works as intended when SIZE(A)>1. Is there a nifty trick to make this work when SIZE(A) is 1?
2. Options 2 and 3 show a different behavior - is this a bug with the simultaneous use of * and the G0 edit descriptor?

0 Kudos
3 Replies
Highlighted
Black Belt
45 Views

#2 and #3 should not be the same.  F2008 10.4p6

Whenever format control encounters a data edit descriptor in a format specification, it determines whether there is a corresponding effective item supplied by the input/output list.  If there is such an item, it transmits appropriately edited information between the item and the record, and then format control proceeds.  If there is no such item, format control terminates.

In the case of #3, the format control doesn't encounter a data edit descriptor without a corresponding effective item (because the repeat count matches the number of items) so it trundles all the way to the end of the specification, while the unlimited repeat count is effectively the same as if the repeat count was a very large number - so extra data edit descriptors are encountered.

I'd just do this using non-advancing input.

WRITE(*,FMT='("[",*(G0,:,","))', ADVANCE='NO') A
WRITE(*,FMT='("]")')

I don't use the intel extension thing.  Where necessary I just build the format specification, with appropriate repeat counts or field widths, up manually using internal writes.

0 Kudos
Highlighted
45 Views

FWIW, Variable Format Expressions (#1 in OP's post) were a DEC extension, carried through to Intel. Other vendors rue the day we came up with this feature, and they have caused us no end of trouble over the years as well (due to their being a "callback" during I/O. Think of it as an early form of User-defined Derived Type I/O.

0 Kudos
Highlighted
Beginner
45 Views

Thanks Steve and IanH for the explanations!

0 Kudos