Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by davorg (Chancellor)
on Jun 23, 2009 at 10:10 UTC ( #773937=note: print w/ replies, xml ) Need Help??


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

I suppose the "technical reason" is that it's something we inherited from C. And in C, an array index is actually an offset from the start of the array. The first element is therefore at index zero as it's offset zero elements from the start of the array.

There's no real technical reason why it should still be implemented that way. But generations of programmers expect it to work like that.

Update: Please do not be tempted to use $[ to change the index base. Really, don't do it. Just forget it exists. If you use it you'll just create a maintenance nightmare for the people who look after your program after you.

--

See the Copyright notice on my home node.

Perl training courses


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 tedbradly on Mar 15, 2015 at 23:46 UTC

    I was forced to create an account for this website after finding this thread while searching about perl's ability to adjust its initial index. I had to, because perl programmers are apparently retarded. They needed someone to tell them why 0 instead of 1. Hint: It's an actual reason. It's not a coincidence that almost every programming language in existence uses 0 instead of 1.

    There was an interesting point brought up that the reason belongs to the physical interpretation of an index as a memory offset. While that's a nice coincidence, it's definitely not the reason modern languages still choose 0 instead of 1.

    The reasons mostly swirl around the length of the array.

    1. Modular arithmetic works with length

    If you ever found the need to map an integer into an array, i%L will give it to you instead of the uglier (i-1)%L. For this reason, and possibly others, in general, doing looping/procedural algorithmic design for arrays that start with 1 can be a pain. I would wager that an index doubling as a distance from the beginning also supports this point as that distance might naturally come up in an algorithm.

    2. The style of specified boundaries argument (Dijikstra)

    First, he establishes that of the way to specify a range ) ( and (), the ) is superior: <p>2.1 L-0 = L but L - 1 != L <p>2.2 For two adjacent subsequences, the upper bound of one is the lower bound of the other. <p>These things happen if you choose (0, L as well. But as he points out, if there ever is a natural starting point, you must always denote your range unnaturally as (p-1, ...]. Hence, we prefer ) over (.

    2.3 He points out that the other ways are associated empirically with error.

    These seem to justify [). Then the style argument comes about easily: [0, L) is prettier than [1, L+1).

    3. Symmetry about the "entry point"

    Consider an array as a chain in memory. With the starting index 0, the counting with respect to it represents a distance from that starting point -- how much you are in the array or out of it. -2 is 2 out of it. 2 is 2 into it. Similarly, lobbies tend to be known as floor 0 with those beneath being P1, ... (instead of -1, ...) and those above as (1, ...).

      > I was forced to create an account ... I had to, because perl programmers are apparently retarded.

      Thats nonsense, you don't need an account for trolling. It's called Anonymous Monk.

      Any idiot world wide can ask and try to answer the most stupid questions here ( "apparently" ).

      Pity you spoiled possibly good arguments with your bad behavior...

      ... cause the less "retarded" Perl programmers won't feed you now in this 6 year old thread. :)

      BTW: please use <code> tags to protect formatting, [...] is used for linking.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://773937]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2015-08-31 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (352 votes), past polls