On Thu, Jul 19, 2007 at 06:13:56PM +0900, Adrian H. wrote:
(taken off-list since wandering way OT
Understandable.
News to me. Would you point me at more information on the topic,
please?
In the perl world we have the $[ variable (see perldoc perlvar for
gory details) that resets the index of the first element. As the
documentation says “Its use is highly discouraged.”. I’d imagine that
it’s primary motivation for inclusion was to make compatability with
awk easier in early perl’s (which also has 1 indexing).
I didn’t know about the likely awk connection in the existence of $[,
and
didn’t know early Perl used 1-indexing. I guess I learn something new
every day.
When I said something was news to me, however, I meant specifically your
statement that the “fix” was global rather than per-array. I somehow
read that as “it applies globally, and you can’t make it apply per
array”, rather than how I now realize you meant it. When you first
mentioned that, I thought you were saying there was something like
perhaps a pragma that changed the way Perl handled arrays, or something
like that.
. . . but yeah, I see now you were just referring to the fact that $[ is
a global variable. Of course, there’s always the local hack:
{
local $[ = 1;
# do stuff
}
$[ = 2;
print @array[5], "\n";
would produce
6
5
4
Spooky action at a distance. Scary.
A little weird, to be sure – but awfully flexible if you need it, I
suppose. Probably the most disturbing thing about it for me is the fact
that, for each of the changes to $[ in your code snippet, that changes
the behavior of all arrays. Downright terrifying.
Fortran (or those versions that support it anyway) allow you to
specify the indices of a particular array - so for example you could
have
real temperature(-100:100)
to make an array of real values with indices from -100 to 100. So you
can make the indices of your array match the indices of your domain -
if that would make your life easier
I don’t know any Fortran, so the talk of Fortran and arbitrary array
ranges is entirely new to me – and fascinating. The various
differences
of feature sets between languages really teaches some interesting
principles, I find.
See http://www.stanford.edu/class/me200c/tutorial_77/10_arrays.html
for some examples.
Thanks.