P is for Practical PerlMonks

### Re: why the array index has to start at 0??

by kubrat (Scribe)
 on Jun 23, 2009 at 11:06 UTC ( #773965=note: print w/replies, xml ) Need Help??

in reply to why the array index has to start at 0??

That's actually a very good question. The most common explanation you'll find is because the index represents an offset from some memory address, so naturally the address of the first element will be at offset 0. But there's another reason which I came across some time ago and was very well explained by some mathematician. It was something to do with boudary checks and the argument was that i<10 is better than i<=10. Unfortunately, I can't remember the details nor can I find the web page.

There are some situations where having the index start at 1 is needed like in some matrix computations if I am not mistaken. But that's not a problem in any language.

Update: It was really bugging me that I couldn't recall who put the really good range argument, so I kept looking for it and here it is:

When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number.

The argument was put by none other but E.W. Dijkstra

Taken from http://lambda-the-ultimate.org/node/1950

• Comment on Re: why the array index has to start at 0??

Replies are listed 'Best First'.
Re^2: why the array index has to start at 0??
by Marshall (Abbot) on Jun 23, 2009 at 13:20 UTC
YES! this has some mathematical justification aside from "how the machine works"!

On another point, I just finished a 'C' project yesterday and I can barf out these C style for(;;) loops with code using [\$i] indicies easily.

I think the BIG point here since we are talking about Perl, is who cares? A HUGE factor in Perl is that we don't care! Or for the most part! Consider: foreach my \$thing(@array){..blah..} Normally for the most part, \$array[0] vs \$array[1] doesn't matter because it never comes up in the code. Perl also reduces the dreaded "off-by-one" error possibility.

Re^2: why the array index has to start at 0??
by ikegami (Pope) on Jun 23, 2009 at 15:31 UTC

It was something to do with boudary checks

If you want to check if a value X is in [0,N) — something that must be done very often — X % N == N will do. If N is a power of two, simpler X >> log(N) would do.

If on the other hand you were using 1-based indexes, you'd have to check if X is in [1,N], and that would require (X-1) % N == (X-1).

Many more operations are simply more natural with 0-based indexes. See Re^3: why the array index has to start at 0?? for some real-life examples.

Re^2: why the array index has to start at 0??
by Zen (Deacon) on Jun 23, 2009 at 14:47 UTC
This argument by Dijkstra is silly. Yes, I said it. Doesn't matter who says it; if the argument is aesthetic, that's not a reason. Consider:

N1..Nn (N sub 1 to N sub n)

My notation is "nicer," therefore it's better? No! The offset argument is better for the 0 discussion, but in the end it comes down to the generally accepted culture of using 0. If you work in a vacuum, by all means set it to be whatever your favorite number is.

Zen:

I don't think it's silly, rather it just moves the question a bit. As you indicate, the question is merely moved to "which is the better way of expressing a range". It seems that Djikstra prefers "0 <= i < N" to "1<= i < N+1". However, he doesn't say why that's any better. I agree that it looks better, but there's another formulation "0 < i <= N" that looks just as good. Why isn't that just as good? He leaves the question open.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Create A New User
Node Status?
node history
Node Type: note [id://773965]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-04-23 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (84 votes). Check out past polls.

Notices?