A zero index is perfectly valid if you think of the index as actually being an offset from the base address of an array. That's the norm in fact, though it varies from dialect to dialect. With the old Jovial language, [0]
actually contained the maximum size of the array. so it started with [1]
.
With some Basic implementations, you can arbitrarily elect to have either 0
or 1
as the first index. And at least one Basic allows you to designate arrays to index fron [n]
to [m]
. where n
and m
can have any integer value, even negative, as long as n
is less than or equal to m
. The value n
them becomes subtracted from the [index]
entered, as index-n = 0
if index corresponds to n
.
But you can do the same thing and more in your own code. You can even reverse the order of an array this way:
dim array(0, abs(m-n));
step=sign(m-n);
if n > m then
base=m
else
base=n;
fi
This code example is actually only partly of one language. I just wanted it to be more readable. The step variable is used to control the apparent direction you are moving in whether positive or negative, and is used to calculate the effective index when going into or coming out of the array, which is actually always positive from [0]
.
${array[@]}
in double quotes. Otherwise, word-splitting will be performed and you will get undesirable results, on inputs likearray=("item 1" "item 2" "item 3")
.