Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://773937]
[Tux]: Corion++ hippo++
[ovedpo15]: it isn't homework. its a module I use at work. The process to add additional module isn't worth it. p.s never heard a university course which teach perl
[marto]: there are many, to this day.
[ovedpo15]: @Corion Yes I use the regex you showed me. its very good regex. although now i need to know that substring unit that comma
marto mad corrections to one around 18 months ago
[ovedpo15]: my code checks the value after that comma, if it isn't valid it will remove it. so I would like to remove the substring after that comma meaning getting the string before comma.
[hippo]: Are you sure that Text::CSV_XS isn't already installed at your work? It's such a useful module that it might well be there.
[marto]: pointing out the advantages of cpan modules is well worth in, as both developer and $client/$company benefit greatly
[Tux]: $src =~ m{^(.*),(.*)$/ and $2 !~ $valid and $src = $1;
[Tux]: s,/,},

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-05-27 10:47 GMT
Find Nodes?
    Voting Booth?