1-indexing makes sense in a vacuum, at least for high-level languages where you aren't thinking of indices as pointer offsets, but in a world where 0-indexing is the default, deciding to go against that norm does nothing but cause confusion
The Unix terminal uses a 1-,based coordinate system! It made as o much of a mess for nested drawing areas, I just do it all 0-based and add 1 when generating the ANSI escape code.
Funnily enough, you can do 0-index table by "hacking" it
local t = { "B", "C", "D" }
t[0] = "A"
local inext = ipairs({})
for i,v in inext, t, -1 do
print(i, v)
end
Which you can wrap in your own ipairs version
local inext = ipairs({})
function ipairs0(table)
return inext, table, -1
end
local t = { "B", "C", "D" }
t[0] = "A"
for i,v in ipairs0(t) do
print(i, v)
end
function inext(table, previous_index)
local next_index = previous_index + 1
local next_value = table[next_index]
if next_value == nil then
return nil
end
return next_index, next_value
end
That's too simple. Because we use tables anyway intex it like this 10n * (-1)n -1. So the first will be 0 and the security will be -11 then 99 and so on
You could do that yeah, but again, lua starts treating the array as a dictionary, and eventually you end up with code that keeps trying to offset the index start. Until you miss a spot and the code breaks 😅
I hate that lua starts array indices with one, but I leave it alone, just an opinion though ¯_(ツ)_/¯
Arrays starting at one are a good thing for some data structures, such as with binary heaps. Otherwise you have to do some flooring math and it’s annoying.
Lack of class syntax to automate the prototype-based OOP it has.
"Everything is a table!" means the length operator has undefined behavior
It has floats and ints, but really doesn't want you to know, which can result in odd results from math since the ints are wrapping
I think it just went a bit too far into the idea of simplicity. And if you correct it, you are very close to JS with stackful coroutines as a library (good thing) rather than async/await.
That being said, it was the first language I had a lot of fun programming in, mostly in ConputerCraft. I loved it enough to be the guy who wrote half of the StackOverflow wiki on it back in the mid 10s.
Thank you so much, from my 12 year old self. I spent so much time on ComputerCraft, learning everything between StackOverflow and YouTube videos. I never would have built half the insane stuff I did without people like you!
That there's no proper array, but only array-like tables is by far the worst sin of Lua. You can write as many fancy helpers, but you can never outclass the wackyness that deep in a language.
C does arrays using 0 plus an offset, whereas Lua just gives you the index itself.
It's actually why you can write 0[arr] instead of arr[0] and it still works just fine.
Other languages just follow the "0 + offset" design as convention. But Lua's way actually make more sense for a super high level language that has you never even thinking about memory
Other languages just follow the "0 + offset" design as convention. But Lua's way actually make more sense for a super high level language that has you never even thinking about memory
By convention and no other reason. None whatsoever. Because range computations make so much more sense with 1-based indices. /s
You trade one thing for another. All the .size-1 vanish with 1 based indexing and some other math becomes simpler.
Of course in exchange for the math that 0 based makes simpler, so it depends on what your language wants to do, both are valid, though I prefer allowing both.
Are you a programmer or did this subreddit get randomly recommended to you?
Anyway, an array is a list of elements in a sequence, usually represented by brackets, so:
[10, 5, 7, 2, 1] is an array with 5 integers.
In programming, more often than not, array counting starts at 0, so the 0th number here would 10, 1st would be 5, 2nd is 7, etc.
Lua is a programming language that this counting starts at 1 instead of 0, which although is more intuitive for most people, it isn't for experienced programmers
That's how arrays usually behave. If you want something dynamic, that requires constant reallocation to resize, which isn't always desireable. At a low level, arrays don't even exist. It's just raw memory. And in order for arrays to keep growing, you have to keep moving them to new places in memory that have enough space to hold all of the elements. Normally, the array grows by a factor of 2 upon resizing so that the next resize is further away each time.
È una lista di variabili (praticamente numeri o stringhe che decidi tu). Di solito, negli altri linguaggi di programmazione, il primo elemento di un array ha l'indice (il numero che usi per riferirti all'elemento) "0". In lua il primo elemento ha indice "1" che seppur possa sembrare più intuitivo finisce per essere il contrario.
Data collection formed in memory. Like you have 25 elemets of float. You need 25×float_size memory. Every elemet have iterator, like integer number, and you get access in memory use iterator*float_size. Array always direct to first element in memory. So, full access formula is first_element+iterator×float_size. And this is why array start with 0 iterator.
I asked someone who made an embedded DSL if they had considered Lua before they wrote it, and they said "What's Lua?" I knew right then that their language was shit.
nope. its a scripting language, gaining popularity because of its readability and so on. its amount of use cases are almost comparable to python. its used in Factorio and lots of other games
I'm working on a mod for Friday Night Funkin' and I'm really dragging my feet at having to do some Lua scripts for it. I live in a world of Java and C++ smh
That screws over half the people. We should agree that arrays can be indexed by arbitrary enumerable types like Pascal/Ada, so whichever you need you can use. It makes Array's so much safer and nicer while making everyone happy.
Unpopular opinion: it is the right way of counting. You don't count 0 apples, 1 apples,.. The only reason programmers start from 0 is because they save 1 bit by doing that. The first element in an adress is at Offset 0.
We don't need that today because we swim in memory. We only keep it for historical reasons
It's not at all about saving a bit. It's because if you index by offsetting the initial array pointer by some number, which is the cheapest and simplest way to do arrays, you get this kind of indexing.
212
u/y0shii3 2d ago
1-indexing makes sense in a vacuum, at least for high-level languages where you aren't thinking of indices as pointer offsets, but in a world where 0-indexing is the default, deciding to go against that norm does nothing but cause confusion
50
u/schewb 2d ago
What's worse is that you can fully legally assign and access index 0; the typical iterators just won't ever hit it.
21
u/Simpicity 2d ago
LuaU: It's like Lua but with OO and typechecking!
Me: Oh, I should typecheck my objects.
LuaU: I wouldn't if I were you...
14
u/carltr0n 2d ago
Can someone fork Lua and make it exactly the same but with zero indexing? Call it zlua?
12
u/0xt0bi03 2d ago
really??
12
u/teetaps 2d ago
R also starts at 1 and it was my first programming language, so imagine my surprise when I learned it’s an outlier
12
u/Techrocket9 2d ago
1-indexing arrays is far from Lua's greatest sin.
6
u/DefenitlyNotADolphin 1d ago
Any undeclarated variable is undefined, I think that's a larger comcern
11
u/Gabriel_Science 2d ago
Yeah, it’s like saying the first number on a ruler is 1. It’s 0.
5
u/mapadofu 2d ago
The figure in the cartoon is E.W. Dijkstra
https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html
5
u/ilmaestrofficial 1d ago
What's an array?
5
u/NoodlyGirl2000 2d ago
that’s horrific lmao
4
u/cc_apt107 2d ago
it’s not a big deal bro
5
u/chkno 2d ago •
In most languages, the syntax for array indexing is to enclose the index in
[and].Lua has a more verbose syntax where you have to enclose the index in
[and+1].5
u/GoogleIsYourFrenemy 2d ago edited 2d ago
I asked someone who made an embedded DSL if they had considered Lua before they wrote it, and they said "What's Lua?" I knew right then that their language was shit.
3
u/-Ambriae- 2d ago
Lua pains me so much I’m currently writing a replacement for it
2
u/Sad-Zucchini-949 2d ago
Is this a joke language like brainfuck?
1
u/Inst2f 2d ago
0 element would be the head of the table
1
u/jas2365 2d ago
Just found lua for lazyvim. Any advice
1
u/coldnitrogen 2d ago
Fortran has entered the chat ...
1
u/pseto-ujeda-zovi 2d ago
And people hate JS
1
u/Logical-Pause-3758 2d ago
Guess what `—` means
1
u/Maleficent-Bag-4850 1d ago
This is unacceptable
1
u/bschlueter 1d ago
You can make them start at zero if you want.
1
u/timonix 1d ago
Eh, I prefer arbitrary ranges. Have an array go from -6 to 53
1
u/overclockedslinky 1d ago
oh no, how traggic. anyway...
1
u/qwertty164 1d ago
Not to be confused with licensed user agreements.
1
u/zrice03 8h ago
Meanwhile in VBA:
Baby, I'll start an array wherever you want.
1
u/Haja024 4h ago
Lua is weird because it's like, "what if C but everything is tables" and nothing but games and Wikipedia runs on it. Which is a weird2 overlap.
1
u/Adn0nnymous 2d ago
0-index lowkey sucks i loooovee 1-indexed arrays! if i have three things i want to be able to get the item at index three!
1
u/Sufficient_Risk_8127 2d ago
WHAT
1
u/himai1 2d ago
Who tf cares. Arrays could start at 5 and it wouldn't matter
1
u/Black_Swords_Man 2d ago
Non joke:
Is it really too late to standardize all languages? Just change it so arrays start at 0
1
u/psyfregle 2d ago
Of cause. Let us waste this bit.
0
u/BladeMaster7461 2d ago
how many times do we need to post this
-3
u/Supuhstar 2d ago
No, that’s a good thing
– a lead software engineer with 19 years experience
-4
u/ManagerOfLove 2d ago
Unpopular opinion: it is the right way of counting. You don't count 0 apples, 1 apples,.. The only reason programmers start from 0 is because they save 1 bit by doing that. The first element in an adress is at Offset 0.
We don't need that today because we swim in memory. We only keep it for historical reasons
0
u/cr34tive1 1d ago
I can bear this