Because back when they were putting line numbers in The Bible, computer scientists had not yet invented the fad of counting from 0. So why should Perl buck such a long tradition when it comes to line numbers?

Update

Although they call it "chapter and verse", "verse" means "line" or, as one dictionary put it, "a single metrical line in a poetic composition; one line of poetry".

And now that I've thought about it some more, I think a more interesting question is why we index arrays starting at 0. Well, that is controversial enough of a thing that Perl supported $[ to let you pick whether to start counting from 0 or 1.

Perl primarilly got the "index starting from 0" from C but C got that idea for a rather specific and obscure reason. In C, array[n] is just syntatic sugar for *(array+n) and it only makes sense for a pointer to point at the first element in its list. So it only make sense to use array[0] to reach the first element in this situation.

Note that this unnatural (pun intended) convention has lead to several problems by causing tension between the numbering humans expect and the numbering C expects. A good example of such problems is that localtime requires you to add one to the month number before you display it. Another is having to distinguish $#array from 0+@array in Perl.

Like most humans, I'd rather count from 1. Unlike some programmers, I'd also rather index starting at 1. I don't like having contention between indexing and counting. Even Perl starts counting from 1 when counting from the end of a list or string [ $array[-1] and substr($s,-1) ].

                - tye ("And now I'd like to quote from Revelations 0:0...")

Thanks to sauoq for reminding me that the deprecated special variable I was thinking of was $[ and not $#.


In reply to Re: Why is $. not zero-based? (holy counting, Batman) by tye
in thread Why is $. not zero-based? by Cody Pendant

Title:
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, details, 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, summary, 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.