Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Perl::Improved Volume 0, Number 0

by perrin (Chancellor)
on Aug 26, 2004 at 17:57 UTC ( [id://386105]=note: print w/replies, xml ) Need Help??


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

The numbering of array indexes and other things in Perl start at 0. This is dumb.

$[ = 1;
Now they start at 1. However, that would be dumb.

Replies are listed 'Best First'.
Re^2: Perl::Improved Volume 0, Number 0
by diotalevi (Canon) on Aug 26, 2004 at 19:18 UTC

    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.

      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]
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://386105]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found