Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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        

In reply to Re^2: why the array index has to start at 0?? (Perl!) by tye
in thread why the array index has to start at 0?? by targetsmart

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Discipulus]: good morning folks!

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (5)
    As of 2018-05-24 06:44 GMT
    Find Nodes?
      Voting Booth?