I don't understand the rationale behind the decision of this part of Lua. Why does indexing start at 1? I have read (as many others did) this great paper. It seems to me a strange corner of a language that is very pleasant to learn and program. Don't get me wrong, Lua is just great but there has to be an explanation somewhere. Most of what I found (on the web) is just saying the index starts at 1. Full stop.
It would be very interesting to read what its designers said about the subject.
Note that I am "very" beginner in Lua, I hope I am not missing something obvious about tables.
t = { [0] = "a" }; print(t[0])
– Fraudint a[] = { 5, 7, 8, 3 }; for( int i=0; i<32; i++ ){ do something with a[i%4] }
Very simple, compared to the ugly a[1 + i%4] – Prevent__set
and__get
since those metamethod, just don't exist.__newindex
and__index
exist though. However those only get called if an index is missng or are newly created. So you will have to create a proxy to a protected data structure. – Popeyed0 <= i < N
vs.1 <= i < N+1
- somehow he insisted the ranges be bookended by asymmetric<=
one side and<
on the other. These are array indices, not PDFs and CDFs, so bounding it the way they do it in statistics make very little sense. If he's fair he would've written0 <= i < N
vs.0 < i <= N
, and one can see it comes down to a matter of preference……... – Vacatefor(…; ….; …)
loops, I write most of myawk
for
loops like ::::::::::::::::: :::::::::for (x = 0; x++ < N; ) { …. }
(sinceawk
is also 1-based indexing) …. i don't see how that's any harder to read than the 0-based ones. If anything, it's eaiser to read, since this approach eliminated the need for the 3rd argument, and consolidated the index incrementing onto boolean condition in the middle. – Vacate