3 ms·
Lua, the oh so nice "rebel" among languages, uses 1 as the first element in its indices :)
by hackermom 15y ago
Lua, the oh so nice "rebel" among languages, uses 1 as the first element in its indices :)
- lysol 15y agoNasty. But it's the same for substring text offsets, array indices etc in Postgres.
- MattJ100 15y agoNasty? In a language without pointers it makes total sense. An index is no longer an offset from an array (or string) pointer, so the first element being 1, the second being 2, just makes more sense. Sure, it's a departure from mainstream languages, but in my opinion it's a perfectly sensible one.
- masklinn 15y ago> Nasty? In a language without pointers it makes total sense. An index is no longer an offset from an array (or string) pointer, so the first element being 1, the second being 2, just makes more sense. Not necessarily. The reasoning in e.g. Python is that the index is not to an element, it's to one of the intervals around an element. The "first interval" (right before the first element) is 0, which naturally leads to -1 being right before the last element, thus arr[-1] being an array's last element, and arr[:-1] being everything but the last element.
- deleted 15y ago[deleted]