Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

by biohisham (Priest)
on Jun 23, 2009 at 22:06 UTC ( #774189=note: print w/replies, xml ) Need Help??

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

let's look at it this way... we can access an array both ways, from the end as well as from the start ....accessing the array from the end you use -x where x is any number that corresponds to the array element position that we wanna capture from the end. so, look at this line now and tell me if 0 was not there will it not have been bizarre that we are interfering with laws of Algebra???
4,3,2,1,0,-1,-2,-3,-4 find that 0 is central to preserve and make intact your brains from having to dare to change the $[ or having to think where the central "Head" of the array is? since the "First in everything is the head" and a first element in an array is its head, and a first number in the one the makes both sides of the sign equated is the big round fat 0

Replies are listed 'Best First'.
Re^2: why the array index has to start at 0?? (Perl!)
by tye (Sage) on Jun 24, 2009 at 05:03 UTC

    Actually, you bring up points that lead me to prefer that Perl index such things starting at 1. I understand the original quoted justification for using 0. And it makes sense particularly in C++, where we have pointers to arrays and pointer arithmetic and "beg <= i < end" ranges.

    We don't have any of those things in Perl, so I find the argument pretty pointless when applied to Perl.

    But there are several relevant things we have in Perl. 0 is false. $x[-1] is the last element of an array. $x[-0] can't sanely be made to be the last element of an array. By starting at 0, index() fails by returning a true value and is forced to return "0 but true" for one success case. I'd be happy for $x[undef] to return undef, preserving the undefinedness. This becomes easy if $x[0] is likewise 'reserved'.

    And, in Perl, we usually don't write:

    for( my $i= 0; $i < @array; $i++ )

    We are much more likely to write something of the form:

    for my $i ( x .. array )

    And by starting at 1 we don't need the ugly $#array addition and just write what makes so much more sense to humans:

    for my $i ( 1 .. @array )

    Give a child a handful of items and ask them to hand them back to you, numbering them as they go. They won't number them from zero to $N-1. The first item will be number 1, of course. That is why we call it the "first" item, the "1st" item, not the zeroth item. "zeroth" is a word you almost never hear from ordinary citizens.

    I expect to hear "zeroth" used in connection with unbounded sequences, for example: "the zeroth power of 2". Perl arrays are not unbounded. They are indexed with values from 0..$#array and -@array..-1. Ugh. It would be so much nicer to have them indexed with values from 1..@array and -@array..-1. I would also appreciate the separation between those ranges so that an off-by-one error in one range can't accidentally land you in the other range.

    I appreciate the elegance with which K&R resolved the long-running argument for the C language, by defining array[i] as a syntactic alias for *(array+i) and thus making array indices unambiguously offsets and therefore something that must start from 0.

    I appreciate the elegance of "beg <= i < end" range definitions. I've used them quite a bit in C++. I've even used them in Perl code. But part of the point of such things is that 'end' is defined as 'the mythical item just off the end of our list'. $N+1 makes perfect sense as that. So I find "1 <= i < N+1" an even clearer representation of such ranges. It even leads to more regularity in defining such ranges, because they all become "start <= i < start+length" (which might lead you to write "1 <= i < 1+N").

    So I've long found the arguments much more convincing for starting from 1 in Perl. Perl isn't glorified assembly (how I've heard many people refer to C) which is why it doesn't have pointer arithmetic which is also why it should number things like real humans do, not like how machine language would.

    As to "zero as a most natural number", I think that belittles the fundamental innovation that was its invention/discovery long after 1 and its followers were being heavily used.

    But all of this is pretty academic (as in "useless") in relation to Perl. Perl long ago standardized on numbering things starting from 0. That decision is not easily undone (or redone).

    - tye        

      Except wouldn't that be:
      for my $i ( @array )

      Elda Taluta; Sarks Sark; Ark Arks

        Yes, (but s/\$i/\$e/ :) we are even more likely to avoid indices altogether in Perl. But how we avoid using indices casts little light on how indices should be defined, so it doesn't really apply to my argument. It helps with the "we don't care as much in Perl" point. Since it is all academic at this point, I'm less concerned with how "important" it is which index scheme fits Perl best.

        - tye        

        Iterating over the elements is obviously preferred, but it's not always possible. Sometimes, you need to iterate over the indexes.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://774189]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2018-04-25 22:37 GMT
Find Nodes?
    Voting Booth?