Home > Archive > Fortran > June 2005 > Zero-size array literals
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Zero-size array literals
|
|
| Rich Townsend 2005-06-03, 3:57 am |
| Hi all --
How does one express zero-sized array (vector) literal in Fortran
90/95/2000. Intuition would suggest something of the form (//), but what
then defines the type of the array?
cheers,
Rich
| |
| Dave Seaman 2005-06-03, 3:57 am |
| On Thu, 02 Jun 2005 22:38:42 -0400, Rich Townsend wrote:
> Hi all --
> How does one express zero-sized array (vector) literal in Fortran
> 90/95/2000. Intuition would suggest something of the form (//), but what
> then defines the type of the array?
Use an implied DO, such as
(/ (0.0, i=1,0) /)
which produces an empty array of type REAL.
--
Dave Seaman
Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
<http://www.commoncouragepress.com/i...book&bookid=228>
| |
| Walt Brainerd 2005-06-03, 3:57 am |
| Dave Seaman wrote:
> On Thu, 02 Jun 2005 22:38:42 -0400, Rich Townsend wrote:
>
>
>
>
>
> Use an implied DO, such as
>
> (/ (0.0, i=1,0) /)
>
> which produces an empty array of type REAL.
>
Just to be clear: (/ /) is not allowed; what is
between must be a *list* of things, which cannot
be empty. Oops, g95 doesn't catch the error.
Another way is to use an empty section of another array:
program empty_array
real, dimension(99) :: a = 99
print *, "x", (/ a(1:0) /) , "x"
! ^^^^^^
end program empty_array
--
Walt Brainerd +1-877-355-6640 (voice & fax)
The Fortran Company +1-520-760-1397 (outside USA)
6025 N. Wilmot Road walt@fortran.com
Tucson, AZ 85750 USA http://www.fortran.com
| |
| Richard E Maine 2005-06-03, 4:00 pm |
| In article <d7ofra$ekn$1@scrotar.nss.udel.edu>,
Rich Townsend <rhdt@barVOIDtol.udel.edu> wrote:
> How does one express zero-sized array (vector) literal in Fortran
> 90/95/2000. Intuition would suggest something of the form (//), but what
> then defines the type of the array?
Excellent question. And you have hit upon exactly the reason for the
problem. (What establishes the type). In f90/f95 you have to use
awkward workarounds like those mentioned by Dave and Walt. The zero-trip
implied DO is the "classic" answer, but I sure think it ugly and
nonintuitive. You add a DO loop, but then make sure that it doesn't
actually contribute any elements because you are really using it just as
a way to establish a type? Yukk. I'll note that zero-trip implied DO
loops in array constructors have several subtleties that required
multiple iterations of interpretations. Most of the subtleties involved
things uglier and more subtle than the examples here. A "favorite"
involved using a function result to establish a character length...when
the function was invoked zero times. Ouch. You "obviously" can't do
that, but drawing the exact line between what you can and can't do was
very difficult.
F2003 has a syntax that addresses this. The syntax was introduced for
other reasons, but it turns out that it gives a (relatively) simple form
for a zero-sized array constructor as a side benefit. A zero-sized
default real array in f2003 could be written as
[real::]
(You can also use the (/ and /) in place of the [ and ], but I prefer
the square brackets). In place of the "real", you can have any type
specifier. A favorite example of mine is something like
[character(16):: 'tom', 'dick', 'harry']
To write something like that in f90/f95, you'd have needed to pad the
strings all to the same length. The type specifier establishes what the
type parameters for the constructor are, so that they don't have to be
inferred from the elements.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
| |
| Andy Vaught 2005-06-03, 4:00 pm |
| On Thu, 2 Jun 2005, Walt Brainerd wrote:
> Dave Seaman wrote:
>
>
> Just to be clear: (/ /) is not allowed; what is
> between must be a *list* of things, which cannot
> be empty. Oops, g95 doesn't catch the error.
It does now...
Thanks,
Andy
|
|
|
|
|