lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Duck wrote:
[...]
> Hmmm. A "pun of contradiction" there. Natural use of arrays means to
> start at one, of course. (Since natural numbers do.)

Actually, I thought this for years, and then discovered when writing
WordGrinder that 1-based arrays is actually a fairly major inconvenience.

The problem is that when you deal with 1-based arrays, offsets and indices now
become *different*. With 0-based arrays, they're the same.

For example, let's take a look at the string:

"aspidistra"

The index of the first i is 4. Now we take a substring starting from there
extending to the end of the string. The substring is:

"idistra"

The index of the *second* i, in this substring, is 3. Therefore, one might
assume that the index of the second i relative to the *overall* string is 4+3
= 7, right? Wrong. You can't add indices. You can only add offsets. So, what
you have to do is to convert the substring index into an offset, by
subtracting 1 from it, and then do the addition. 4+(3-1) = 6.

When you deal with 0-based arrays, this entire problem vanishes to such an
extent that most people don't realise that indices and offsets aren't the same
thing. (In fact, languages like C don't really use indices --- all array
'indices' are really offsets from the beginning of the array.)

The end result is that all my string handling code has to be full of -1s.
Ironing out all the off-by-one errors was a pain.

- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHJHrCf9E0noFvlzgRAockAKCMHoiwcI/7BifDfQ/IDnhHqJuxpACfXRea
HySl5+Dp/v0hLHteQx2XQk4=
=Ib15
-----END PGP SIGNATURE-----