News for hackers. Stuff that matters.
Re:Why I chose Lua for this blog by WolfeReader on Thursday October 02, 2025 @04:26PM (#45455101)
The 1 indexes are only a difference from what you're used to. Lua was made by mathematicians, who of course wanted to address the first element as 1, the second element as 2, etc.

0-indexing makes sense in the context of C where the index operator is syntactic sugar for pointer arithmetic. In higher-level languages like C# and Python and others, it's pretty much just a leftover habit from C devs that we all got used to.

Global by default is a perpetual issue, agreed.

Re:Why I chose Lua for this blog by DavidVoid on Thursday October 02, 2025 @05:31PM (#45455844)
And a lot of the time it makes the syntax more compact than it would be with 0-indexing.

  for i=1,#arr do
    foo(arr[i])
  end
I don't feel that strongly for or against either way of indexing though, they both have their pros and cons.
Re:Why I chose Lua for this blog by kqr on Friday October 03, 2025 @12:06AM (#45458856)
Perl is usually used with the first element being zero and the same loop would be

    for (0..$#arr) {
        foo(arr[$_])
    }
Whatever you're feeling is not in starting at one.
Re:Why I chose Lua for this blog by gautamcgoel on Friday October 03, 2025 @12:43AM (#45459065)
You are correct that 1-based indexing is the norm among mathematicians, but none of the creators of Lua is a mathematician, AFAIK. You can read about the early history of Lua here:

https://www.lua.org/history.html

Re:Why I chose Lua for this blog by themafia on Friday October 03, 2025 @04:11AM (#45460340)
> The 1 indexes are only a difference from what you're used to

The left handed scissors are only a difference from what you're used to.

> Lua was made by mathematicians

The default value is nil and using nil as an index on a table returns nil. Yet nil + number is not valid and results in a runtime error.

> it's pretty much just a leftover habit from C devs

It's reflective of the fact that these languages are either intended to work with C APIs or are implemented in C itself. This makes writing FFI and extensions _far_ easier than it would be otherwise.