4 ms·
I've sat down to learn Lua a number of times because on the I really like the idea of it (small, fast, safe, etc). It really has quite a beautiful minimalism to
by __david__ 16y ago
I've sat down to learn Lua a number of times because on the I really like the idea of it (small, fast, safe, etc). It really has quite a beautiful minimalism to it.
But each time I try I get to the part where I learn that array indices start at 1 and I get annoyed. Have we not learned anything since BASIC? Sigh.
- JulianMorrison 16y agoWhat do you object to in counting from 1? (Other than it's not traditional.) In C, it's an offset into a typed block of memory, so 0 indexing makes sense. But lua does not deal directly with memory.
- __david__ 16y agoIt has nothing to do with indexing memory--it has to do with 1 based arrays causing "+1" and "-1" to be sprinkled all oever the place. I find Dykstra's argument to be compelling: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW...
- kemayo 16y agoMy experience with Lua hasn't seen this problem materialize.
- tryp 16y agoI found that adopting the native idioms meant never having to notice the 1- based indexing. Everything is a hash table, and if you find yourself using literal numeric keys, there's probably a nicer way to do it. The lua users wiki is full of interesting ideas for tackling the basics in ways that leverage lua's simple flexibility.
- __david__ 16y agoThat is entirely possible. I can tell if someone doesn't know perl or python because they write for loops like a C coder instead of looping over containers. So I can believe that you don't come in contact with that detail very often. It just bugs me that such an obvious wart exists in an otherwise very clean language.
- stcredzero 16y agoWhere "obvious" = something only someone with a foreign viewpoint thinks about. ("foreign" with regards to programming language.)
- silentbicycle 16y agoThe reason the arrays index from 1 is that Lua is designed to pare down to a data description & configuration language for non-programmers, and they felt that starting arrays from zero would be confusing. I don't like it it either, but in practice it's a minor issue. People who write off Lua because of indexing from 1, Python because of the significant whitespace, Lisp for its pares, etc. probably haven't gotten to the really interesting stuff yet.
- pederb72 16y agoActually, Lua's table object is split into two parts. The array part, which is accessed using numerical indices, and the dictionary. It's done like that to enable fast array access, and I do find it an incredible useful feature in Lua. In my experience 1-based indexing is not a problem when you're just writing Lua code. I've sometimes been frustrated when creating Lua bindings to C-modules which involved some kind of array access though.