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

Is this namelist code legal?

4 views
Skip to first unread message

Jerry DeLisle

unread,
Oct 5, 2007, 3:18:30 PM10/5/07
to
Hi,

With the test case given below, is this line valid in the namelist:

x%m%ch(:)(2) = 'z','z','z','z','z','z','z','z',

I observe several compilers fail on this.


Test case:

module global
type :: mt
character(len=2) :: ch(2) = (/"aa","bb"/)
end type mt
type :: bt
integer :: i(2) = (/1,2/)
type(mt) :: m(2)
end type bt
end module global

program namelist_15
use global
type(bt) :: x(2)

namelist /mynml/ x

open (10, status = "scratch", delim='apostrophe')
write (10, '(A)') "&MYNML"
write (10, '(A)') " x = 3, 4, 'dd', 'ee', 'ff', 'gg',"
write (10, '(A)') " 4, 5, 'hh', 'ii', 'jj', 'kk',"
write (10, '(A)') " x%i = , ,-3, -4"
write (10, '(A)') " x(2)%m(1)%ch(2) ='q',"
write (10, '(A)') " x(2)%m(2)%ch(1)(1) ='w',"
write (10, '(A)') " x%m%ch(:)(2) = 'z','z','z','z','z','z','z','z',"
write (10, '(A)') "&end"

rewind (10)
read (10, nml = mynml, iostat = ier)
if (ier .ne. 0) print *, 'First read.' !call abort ()
close (10)
end program namelist_15

Michael Metcalf

unread,
Oct 5, 2007, 5:16:03 PM10/5/07
to

"Jerry DeLisle" <jvde...@verizon.net> wrote in message
news:a6wNi.13$h33.9@trndny02...

> write (10, '(A)') " x%i = , ,-3, -4"

The line above is illegal. It represents an array of arrays (i and m), which
is not allowed in Fortran (see "Fortran 95/2003 Explained", Section 6.13).

> write (10, '(A)') " x(2)%m(2)%ch(1)(1) ='w',"

This line contains a spurious '(1)'

> write (10, '(A)') " x%m%ch(:)(2) = 'z','z','z','z','z','z','z','z',"

This is also an attempt at an array of arrays (x and m).

I would hope that *all* compilers complain about this!

Regards,

Mike Metcalf


Michael Metcalf

unread,
Oct 5, 2007, 5:18:56 PM10/5/07
to

"Michael Metcalf" <michael...@compuserve.com> wrote in message
news:nQxNi.185$gC2.162@trndny09...

>
>> write (10, '(A)') " x(2)%m(2)%ch(1)(1) ='w',"
>
> This line contains a spurious '(1)'
>
or possibly (1:1) is meant (as substring notation).

M.


glen herrmannsfeldt

unread,
Oct 5, 2007, 7:13:33 PM10/5/07
to
Michael Metcalf wrote:

>> write (10, '(A)') " x%i = , ,-3, -4"

> The line above is illegal. It represents an array of arrays (i and m), which
> is not allowed in Fortran (see "Fortran 95/2003 Explained", Section 6.13).

Isn't it just printing out a character string?

>> write (10, '(A)') " x(2)%m(2)%ch(1)(1) ='w',"

(snip)

> I would hope that *all* compilers complain about this!

I hope no compilers do. It might be that the library NAMELIST
routine will complain on reading that in, but that is a different
question.

-- glen

glen herrmannsfeldt

unread,
Oct 5, 2007, 7:24:33 PM10/5/07
to
Jerry DeLisle wrote:

> With the test case given below, is this line valid in the namelist:
>
> x%m%ch(:)(2) = 'z','z','z','z','z','z','z','z',

A very quick look through the standard makes it look fine,
but it would require a more careful look to be sure.

One question about structure (derived type) references in general:
is one allowed to move subscripts around? I know PL/I allows it, but
is x(2)%m(2)%ch the same as x%m%ch(2)?

> I observe several compilers fail on this.

> Test case:

> module global
> type :: mt
> character(len=2) :: ch(2) = (/"aa","bb"/)
> end type mt
> type :: bt
> integer :: i(2) = (/1,2/)
> type(mt) :: m(2)
> end type bt
> end module global

> program namelist_15
> use global
> type(bt) :: x(2)

> namelist /mynml/ x

> open (10, status = "scratch", delim='apostrophe')
> write (10, '(A)') "&MYNML"
> write (10, '(A)') " x = 3, 4, 'dd', 'ee', 'ff', 'gg',"
> write (10, '(A)') " 4, 5, 'hh', 'ii', 'jj', 'kk',"
> write (10, '(A)') " x%i = , ,-3, -4"
> write (10, '(A)') " x(2)%m(1)%ch(2) ='q',"
> write (10, '(A)') " x(2)%m(2)%ch(1)(1) ='w',"
> write (10, '(A)') " x%m%ch(:)(2) = 'z','z','z','z','z','z','z','z',"
> write (10, '(A)') "&end"

Note that / is the standard namelist terminator, instead of the
non-standard &end form. (That form was used in many non-standard systems.)

> rewind (10)
> read (10, nml = mynml, iostat = ier)
> if (ier .ne. 0) print *, 'First read.' !call abort ()
> close (10)
> end program namelist_15

-- glen

Richard Maine

unread,
Oct 5, 2007, 6:36:20 PM10/5/07
to
glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:

> One question about structure (derived type) references in general:
> is one allowed to move subscripts around?

No. A subscript can only be on something that is an array. Now it turns
out that, for example, if x is an array, and y a scalar component, then
x%y is also an array, so x(2)%y means the same thing as x%y(2). But
describing this as "moving subscripts around" is misleading and will
lead you to incorrect conclusions. That's just the wrong way of thinking
about it, even though some cases look like that. If you think about it
that way, you'll never es the difference between the cases that are
allowed and those that aren't. For example, if x is a scalar, and y is
an array component, then x%y(2) could make sense, but x(2)%y is just
nonsense.

> is x(2)%m(2)%ch the same as x%m%ch(2)?

No. Never. For a start, one of them hs 2 subscripts and the other only
has one. So even without the other issue, the ranks would be different.
The other issue is that, as mentioned elsethread, arrays of arrays are
not allowed. In particular, for x(2)%m(2)%ch to make any sense at all,
then both x and its m component must be arrays. In that case, writing
x%m%anything is invalid in the first place. It doesn't matter what the
"anything" is, including whether or not it involves subscripts.

You may not have more than one "part" of a designator with rank>0. That
is a specific prohibition in the language. That's what people are
talking about when they say arrays of arrays.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain

Michael Metcalf

unread,
Oct 5, 2007, 6:54:43 PM10/5/07
to

"glen herrmannsfeldt" <g...@ugcs.caltech.edu> wrote in message
news:reGdnZeKu6LYK5va...@comcast.com...

> I hope no compilers do. It might be that the library NAMELIST
> routine will complain on reading that in, but that is a different
> question.
>

Yes, you're right. I tested this code with Intel 10.0 and it compiled. It
was its run-time system that pointed precisely at the three problem areas. I
was replying too hastily. Formally, it's the contents of file 10 that are in
error.

Regards,

Mike Metcalf


Jerry DeLisle

unread,
Oct 5, 2007, 7:27:32 PM10/5/07
to
I should have been clearer, my question is regarding the validity of the
contents of the namelist file. At the moment gfortran handles this, perhaps as
an extension. :) I will look into providing some additional run time checks.

Thanks very much for the response.

Jerry

Jerry DeLisle

unread,
Oct 5, 2007, 7:31:46 PM10/5/07
to
glen herrmannsfeldt wrote:
> Jerry DeLisle wrote:
>
>> With the test case given below, is this line valid in the namelist:
>>
>> x%m%ch(:)(2) = 'z','z','z','z','z','z','z','z',
>
> A very quick look through the standard makes it look fine,
> but it would require a more careful look to be sure.
>
Looking around a bit we see: C614 (R612) There shall not be more than one
part-ref with nonzero rank. And there are other places like 10.10.1.1.

So, I need to add some run time checks to gfortran.

Thanks,

Jerry

glen herrmannsfeldt

unread,
Oct 5, 2007, 10:58:30 PM10/5/07
to
Richard Maine wrote:

> glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:

>>One question about structure (derived type) references in general:
>>is one allowed to move subscripts around?

> No. A subscript can only be on something that is an array. Now it turns
> out that, for example, if x is an array, and y a scalar component, then
> x%y is also an array, so x(2)%y means the same thing as x%y(2). But
> describing this as "moving subscripts around" is misleading and will
> lead you to incorrect conclusions. That's just the wrong way of thinking
> about it, even though some cases look like that. If you think about it
> that way, you'll never es the difference between the cases that are
> allowed and those that aren't. For example, if x is a scalar, and y is
> an array component, then x%y(2) could make sense, but x(2)%y is just
> nonsense.

Yes, it makes more sense to move to the right.

It seems, though, that PL/I allows them to move right or left:

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/ibm3lr30/8.14.1

>>is x(2)%m(2)%ch the same as x%m%ch(2)?

> No. Never. For a start, one of them hs 2 subscripts and the other only
> has one.

Oops. That isn't what I meant...

It was supposed to be x%m%ch(2,2)

> So even without the other issue, the ranks would be different.
> The other issue is that, as mentioned elsethread, arrays of arrays are
> not allowed. In particular, for x(2)%m(2)%ch to make any sense at all,
> then both x and its m component must be arrays. In that case, writing
> x%m%anything is invalid in the first place. It doesn't matter what the
> "anything" is, including whether or not it involves subscripts.

Yes, I meant arrays of structures of arrays.

It seems that C officially uses arrays of arrays instead of
multidimensional arrays, but the result is most of the time
close enough for me.

> You may not have more than one "part" of a designator with rank>0. That
> is a specific prohibition in the language. That's what people are
> talking about when they say arrays of arrays.

So for array of a defined type with an array inside, which I mean
instead of array of array, sometimes you can move subscripts to
the right, and sometimes you can't?

For x(2)%y you can say x%y(2). For x(2)%y(2) you can't say x%y(2,2)?

-- glen


Richard Maine

unread,
Oct 5, 2007, 10:04:50 PM10/5/07
to
glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:

> So for array of a defined type with an array inside, which I mean
> instead of array of array, sometimes you can move subscripts to
> the right, and sometimes you can't?

I decline to even talk about "moving subscripts" in such a context. As I
said before, I think it the wrong concept.

Jan Vorbrüggen

unread,
Oct 12, 2007, 4:03:05 AM10/12/07
to
> So for array of a defined type with an array inside, which I mean
> instead of array of array, sometimes you can move subscripts to
> the right, and sometimes you can't?
>
> For x(2)%y you can say x%y(2). For x(2)%y(2) you can't say x%y(2,2)?

I think what you are describing is what IDL and its relatives are doing, and
Fortran doesn't do it that way.

IDL basically allows you to specify an entity that has one or more dimensions,
and to then stick the indices at the end of the entity all in one bunch. That
is, you conceptually first assign the values of the entity to a temporary, and
then index that temporary. This allows you to index items that aren't
indexable in Fortran - my default example is (SHAPE(some_array))(i).

Jan

0 new messages