in reply to Re: Perl::Improved Volume 0, Number 0
in thread Perl::Improved Volume 0, Number 0

Indices should start at one. Its the joining of the meanings of offset and index that leaves us with arrays that start at zero. Offset is a measure of how far to travel from the start and there it makes sense that travelling nowhere from the start leaves you at the start. Calling the first element the zeroth element is just sloppy.

Maybe if this were C or some sort of assembly then I'd think that using offsets makes more sense than indices.

  • Comment on Re^2: Perl::Improved Volume 0, Number 0

Replies are listed 'Best First'.
Re^3: Perl::Improved Volume 0, Number 0
by Courage (Parson) on Aug 27, 2004 at 06:26 UTC
    it seems to me that having indicies starting at 0 makes programming much easier, because this concept removes many playings with adding/substracting 1 when calculating them.

    Imagine you do two-dimension array simulation
    With 1-based arrays you'll need to do something like this:

    $a[3*($i-1)+$j-1]
    and this is equivalent to
    $a[3*$i-2+$j]
    which is dumb
      Wow, I made SO many errors in code examples,
      but those prove my point.
      Well, that would be:
      $a [$J * ($i - 1) + $j]
      as opposed to
      $a [($J + 1) * $i + $j]
      where $J is the highest second dimensional index. (Which if indices start with 1 happens to be identical to the number of indices).

      If indices start at 1, taking a slice till the end of an array can be written as:

      $a [5 .. @a]
      instead of having to write
      $a [5 .. @a - 1]
      or to have to use the $#array form:
      $a [5 .. $#a]
        If indices start at 1, taking a slice till the end of an array can be written as:

        The good solution for that imo would be to have the .. and ... operators as in ruby, that is, 3..8 means an inclusive range, but 3...8 means a range including 3 but not including 8.

        Update: I say this because I've never seen any good example of using $#{} other than a range ending in it.

A reply falls below the community's threshold of quality. You may see it by logging in.