Indices starting at 1 are an abomination.

cephalo

Deity
Joined
Jul 26, 2007
Messages
2,058
Location
Missouri, USA
Ugh. Everytime I have to learn a new language, there's always something that's completely unlike every other language for no good reason at all. With Python it was the god awful significant whitespace, and for lua it's this. Why????? Actually you can start an array at zero, but then you cannot use functions like print or sort on your array because they all assume one. Good luck with that.

Let me tell you how this is going to affect me. I'm about to start a new map script, and because Civ maps tend to wrap in the X direction, lots of stuff that you do involves modulus arithmetic. So if you have a map that is 100x100 tiles and you are on the eastern map edge wanting to continue east and wrap around, you simply add 1 and then say (X = X % width) and it resolves itself. The result will always be a valid X coordinate between 0 and 99. Starting your indices at 1 however totally messes that up. I don't know how I'm going to get my brain around this!

This nightmare is going to recur endlessly for any map scripter, because you constantly have to work with Euclidean coordinates where the origin is at 0,0 and NEEDS to be at 0,0 to make mathematical sense. What a stupid way to ruin an otherwise fine scripting language! Who does this help? I feel sorry for anyone who learns lua as their first programming language because you're never going to be able to model any real mathematical concept, and you will go to your next programming language with the worst sort of habit ever!

Ugh. Storing a 2D map with the origin at 1,1 is something I'm going to have to learn how to do, and ALL the math is going to have to be done differently somehow. I do not look forward to this.
 
Why don't you just subtract 1 before doing the math and add 1 before storing it in the array.
 
Why don't you just subtract 1 before doing the math and add 1 before storing it in the array.

Ok ok, I just needed someone to calm me down. Thanks.
 
Lua was originally a scripting language designed for engineers with little to no programming experience. 1 based indexing was chosen because it is more intuitive to non programmers. The only reason most languages use 0 based indices is that in low level languages, like C, an array is just a pointer with fancy syntax.

Frankly, you are overreacting. Your problem can be solved by adding an extra line of code (x = (x==0 and width or x)) or by subtracting 1 before the modulus, then adding after. It's not that big a deal. It will just take some getting use to. Lots of noobie Lua coders have made this complaint before, and then a couple days later barely even notice.

If it really bothers you, you can always just assign your map data a metatable that replaces reads/writes to the 0th index with reads/writes to the nth index, then continue writing your code as you always did. Better yet, you could make the metatable handle any call to an out of bounds index by performing the modulus and check for you. Then you wouldn't even need to think about it, you could just add any arbitrary distance from a coordinate, and lua would do the work for you. This is the beauty(and sometimes the curse) of truly dynamic languages. If you don't like the behavior of a particular object, you can change it. Just be sure you change it in a way that's transparent to anything else using it.
 
If it really bothers you, you can always just assign your map data a metatable that replaces reads/writes to the 0th index with reads/writes to the nth index, then continue writing your code as you always did. Better yet, you could make the metatable handle any call to an out of bounds index by performing the modulus and check for you. Then you wouldn't even need to think about it, you could just add any arbitrary distance from a coordinate, and lua would do the work for you. This is the beauty(and sometimes the curse) of truly dynamic languages. If you don't like the behavior of a particular object, you can change it. Just be sure you change it in a way that's transparent to anything else using it.

Metatables. Hmm, even if I don't end up doing it this way, this sounds like something I should know how to do.

I have a question that may or may not be related. I noticed that the stock map scripts often use the 'self.thisvariable' in the old python style, but I can't figure out where the 'self' is coming from. Anyone have any insight on this?
 
'self': Combination of two things in lua:
Function calls of the format 'table:f(args)' get translated to 'table.f(table, args)'

Function definitions using the similar format 'function table:f(args)' become 'table.f = function(self, args)'

Metatables: Looks like you'd do something like 'metatable(table).__index = function (self, key)' from what the Lua reference is saying.
 
Indexing arrays starting with 1 is perfectly normal - it is 0 that is odd. The first element of an array is the *first* element, not the zeroth element, thus it should have an index of 1. Consider a mathematical vector X of 3 elements. In math, how are the elements labeled? They are x1, x2, and x3 (where the 1, 2, and 3 are normally subscripts). No 0 in there at all. A 1 dimensional array is just a vector. Now consider a plain old 2 dimensional mathematical matrix. When you index its elements are there any in there that are indexed as element 0,0? No, there are not. The first column of the first row in a matrix is element 1,1. A 2 dimensional array is just a matrix. Deviating from how math works is weird.

The "index" for 0 based arrays is not really an index. It is is an offset from the beginning, as in "how many away from the beginning is it". Index = start at 1. Offset = start at 0 (since the first element is 0 away from the first element). But, sadly, everybody calls them both "index" even if it is just confusing to a lot of people.
 
The standard is to start from 0, because it makes offsets much faster to calculate. Please don't say something about mathematicians expectations of indexes, they are unrelated to the reality of programming as it has been practiced for decades.

There are enough off-by-one bugs in the world without doing something like this. I've never used Lua, but it just took a step down in my view.

Your problem can be solved by adding an extra line of code (x = (x==0 and width or x)) or by subtracting 1 before the modulus, then adding after. It's not that big a deal. It will just take some getting use to. Lots of noobie Lua coders have made this complaint before, and then a couple days later barely even notice.

And then a few months later try to use just about any other language in the world and FAIL MISERABLY.

If you don't like the behavior of a particular object, you can change it. Just be sure you change it in a way that's transparent to anything else using it.

Hahahahaha you're fired.

Actually I fired dynamic language features a while ago.

Why don't you just subtract 1 before doing the math and add 1 before storing it in the array.

Because giving humans more opportunities to make mistakes while programming is redundant?

Yes I'm posting a lot. I'm very annoyed. Pythons significant white-space was ******ed (c.f makefiles), but this is possibly worse.
 
As a personal "taste" I don't like 0's as anything that can evaluate to true or, in arrays/tables matters, that can index/"point" to any value - for the fact that I have to double check these 0's if they're really a NULL/false or something that I will use. On the other hand it really not feels that natural not using 0 on them, eh, I foresee myself debugging a lot and finding out those "expecting 0 index" situations :p

I don't know much of Lua yet but I'm liking what I'm seeing.
 
Metatables. Hmm, even if I don't end up doing it this way, this sounds like something I should know how to do.

I have a question that may or may not be related. I noticed that the stock map scripts often use the 'self.thisvariable' in the old python style, but I can't figure out where the 'self' is coming from. Anyone have any insight on this?

Yes understanding metatables is absolutely crucial for being a good Lua programmer. Even if you don't use them yourself, you should know how they work.

Kobata answered your other question pretty well. 'self' in Lua is basically just a bit of semantic sugar.

Hey, here i can talk a bit, and i have to agree with Cephalo :goodjob:.
Doing something not according to somehow "standards" is always confusing, and here is no need for it.

As i have explained, Lua was not made to be a standard language. It was made to be rapidly useable by people that don't have experience.

The standard is to start from 0, because it makes offsets much faster to calculate. Please don't say something about mathematicians expectations of indexes, they are unrelated to the reality of programming as it has been practiced for decades.

No, it is 0 because that is literally how it is represented in the machine. An array is just a pointer.

1 based indexes are more intuitive to people with 0 programming experience, especially those with a mathematical background. Lua was original designed for people with 0 programming experience and mathematical backgrounds, thus it has 1 based indexes. Roughly 10 years after its creation, game developers noticed that Lua's small footprint and blazing speed made it perfect for embedding in games as a scripting language and its popularity has grown substantially. The original creators aren't gonna go back and deprecate millions of lines of code by changing it 17 years after the fact. You are making way too big a deal out of something that is a non issue once you get use to it, just like pythons whitespace. It's only a big deal to people who don't actually know what they are doing or to people who are too set in their ways to see something good when it comes along.

And then a few months later try to use just about any other language in the world and FAIL MISERABLY.

Uhhh..no? I don't know what to say to this besides you're wrong. I use Lua regularly, along with a host of other languages and rarely have problems.

Hahahahaha you're fired.

Actually I fired dynamic language features a while ago.

/facepalm

That explains a lot.
 
Indexing arrays starting with 1 is perfectly normal - it is 0 that is odd. The first element of an array is the *first* element, not the zeroth element, thus it should have an index of 1. Consider a mathematical vector X of 3 elements. In math, how are the elements labeled? They are x1, x2, and x3 (where the 1, 2, and 3 are normally subscripts). No 0 in there at all. A 1 dimensional array is just a vector. Now consider a plain old 2 dimensional mathematical matrix. When you index its elements are there any in there that are indexed as element 0,0? No, there are not. The first column of the first row in a matrix is element 1,1. A 2 dimensional array is just a matrix. Deviating from how math works is weird.

The "index" for 0 based arrays is not really an index. It is is an offset from the beginning, as in "how many away from the beginning is it". Index = start at 1. Offset = start at 0 (since the first element is 0 away from the first element). But, sadly, everybody calls them both "index" even if it is just confusing to a lot of people.

The thing is, the use of the word 'index' that you're talking about is just an arbitrary name, just like any variable name, while the standard 0 based index is actually mathematically useful and coherent. That is why it is used in every other programming language, its not even an issue with pointers, its that applying math to your index is very common and very useful, and pretending zero doesn't exist is a hindrance in that regard.

It's like saying that the clock starts at 1:00 am and midnight doesn't exist. So what time was it it 49 hours ago? I'm not about to do the math but I bet its an extra line of code in the very least.

Really, this carries over to the common idea that scripting languages open up programming to non-programmers, as if it makes things easier somehow. But things like strong typing and statement terminators are there to help you! Programmers don't want difficulties, they want things easy! That's why those things are there. They make the task of programming less error prone, less ambiguous and faster to do.

Would it be easier to teach someone to speak english without punctuation? Are you helping them by doing that? If somebody wants to program, teach them how!
 
Really, this carries over to the common idea that scripting languages open up programming to non-programmers, as if it makes things easier somehow. But things like strong typing and statement terminators are there to help you!

:goodjob:

Programmers don't want difficulties, they want things easy!

Damn straight. We need protection from ourselves.
 
The ignorance in this thread is palpable. It's like travelling back in time to 1995. Dynamic languages are not scary, duck typing is fine, 1 based indexing is just different. Deal with it or leave, it's not changing.
 
How were they wrong, exactly?

Thousands of extremely successful products have been produced using dynamic languages. Developers have somehow managed to overcome these horrible programming obstacles that you seem to think exist with them, but have been proven time and time again to not really matter.

The language you have done nothing but moan about, Lua, is in use in literally hundreds of top tier games because of its portability, lightweight footprint, and strong performance. I'm sure you know more than the entire games industry though, so they're probably 'wrong' :rolleyes:
 
The language you have done nothing but moan about, Lua, is in use in literally hundreds of top tier games because of its portability, lightweight footprint, and strong performance. I'm sure you know more than the entire games industry though, so they're probably 'wrong' :rolleyes:

Machiavelli on "The end justifies the means" said it all, he aimed at Christianism and hit programming languages.

:D
 
Top Bottom