Struanmcd
|
  |
| Joined: 19 Sep 2008 |
| Total Posts: 369 |
|
|
| 13 Feb 2013 01:17 PM |
I have just started to learn Lua, after experience with other programming languages (Java and JS). One of the first things which stand out is that arrays appear to start on 1, as opposed to 0. Why did the developers of Lua decide to do this? I cant find an explanation on the web, tutorials merely state that the indexing begins at one.
Just to point out, please forgive me if I've made a fundamental error or if I have missed something. Another oddity with arrays is that a lot of people like to refer to them as tables, I assume this is a feature of Lua, but correct me if I'm wrong.
This isn't an ask for help, I understand the principles behind arrays, I was just wondering what everyone else's thoughts were on the wierdness of the indexing.
Regards,
Struan |
|
|
| Report Abuse |
|
|
Sorcus
|
  |
 |
| Joined: 29 Nov 2010 |
| Total Posts: 3775 |
|
|
| 13 Feb 2013 01:22 PM |
Sure you can find it on the web. Why - if I Google it the first link is StackOverflow answering your very question.
http://stackoverflow.com/questions/2785704/why-do-lua-arraystables-start-at-1-instead
Ideally, I would remove this thread but I am going to let this linger around to see if any productive discussion happens in this thread.
~Sorcus |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 13 Feb 2013 01:24 PM |
| The simplest explanation is that humans are accustomed to one-based indexing. |
|
|
| Report Abuse |
|
|
SN0X
|
  |
| Joined: 24 Oct 2011 |
| Total Posts: 7277 |
|
|
| 13 Feb 2013 01:27 PM |
Oh man, lol, I was hoping you were going to lock it so that just before you did that I would get last post and troll.
Oh well.
--
I think they started arrays at 1 just to make the language more easy, and more human. We don't count starting at 0. Maybe Chuck Norris does. But, for example, if these are oranges: O O O we wouldn't say that there are 2 oranges, and count them "zero, one, two". We'd say there are 3.
Yes, Lua was designed to be easy and more human I suppose. Hence all these English self-explanatory keywords- "while", "repeat", "do", "end", "until"... |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 13 Feb 2013 01:31 PM |
> Just to point out, please forgive me if I've made a fundamental error or if I have missed something. Another oddity with arrays is that a lot of people like to refer to them as tables, I assume this is a feature of Lua, but correct me if I'm wrong.
Lua tables aren't like arrays in other languages. In other languages, to create an array you specify the data type and the length to create an array. Once created, you can't add anymore items.
string[] array = new string[10]; // an array of 10 string array[0] = "Hello"; array[9] = " "; array[10] = "world!"; // an error will be thrown, because the array can only store 10 strings, not 11
However in Lua, tables can be resized and can store multiple data types. Additionally, non-numeric indices can be used.
array = {} array[0] = "Hello" array[9] = " " array["SomeRandomString"] = 5 array[true] = false
When people talk about arrays in Lua, they are talking about tables where the indices are integers. You can omit the indices when creating "arrays" in Lua:
{ "Hello", " ", "world!" }
is the same as:
{ [1] = "Hello", [2] = " ", [3] = "world!" }
|
|
|
| Report Abuse |
|
|
Garnished
|
  |
| Joined: 09 Apr 2012 |
| Total Posts: 12695 |
|
|
| 13 Feb 2013 01:32 PM |
| Ehh... It starts on 1 in lots of languages. |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 02:03 PM |
@Garnished
You're right, But it's in languages that aren't normally used in today's businesses. Only COBOL that I know of is still used for businesses related development. |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 02:03 PM |
ARRGH!
*still used for business |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 13 Feb 2013 02:04 PM |
"Lua indices are actually indices. In C when you say index what you really mean is an offset. "
From Sorcus's link. This makes sense, but I'm not sure if it is true.
Anyway, since Lua is the only programming languages I've used tables/arrays with, it's just natural. I don't know if AS2 has them, or how they work, since I only did fairly simple things with it, just a couple flash games.
"If you want to become a Developer or Innovator for CSA, contact me." |
|
|
| Report Abuse |
|
|
Struanmcd
|
  |
| Joined: 19 Sep 2008 |
| Total Posts: 369 |
|
|
| 13 Feb 2013 02:20 PM |
Apologies to Sorcus, my first thought was to post it here to stimulate discussion before searching, but if the aim here is to encourage original content, I'll either check first or post elsewhere in future.
Are there any advantages to using tables over arrays in a language, or vice versa? With arrays, they limit you to a static data structure, and only allows one type of data type. If Lua uses tables, why do arrays exist in other languages? Here's how I interpret it, if you only want to place a certain data type in a data structure, why don't we use tables de-facto? Surely arrays are very limiting. |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 13 Feb 2013 02:24 PM |
Well, I know that on ROBLOX, the environment is very dynamic. I don't know if this is true in other applications of Lua, but I can see it as a good reason to have dynamic tables as well.
Perhaps with those programs using static arrays, or those languages, there's no need for the type of tables you see in Lua?
Although, I do love Lua's tables...
"If you want to become a Developer or Innovator for CSA, contact me." |
|
|
| Report Abuse |
|
|
Sorcus
|
  |
 |
| Joined: 29 Nov 2010 |
| Total Posts: 3775 |
|
|
| 13 Feb 2013 02:31 PM |
@Struan - I wasn't trying to be dissuading, but your original post did say that you couldn't find it on the web. But seems like the thread is going somewhere. Also in my opinion dynamically typed languages and higher level languages should index from 1 simply because there are more people willing to try them and trying them vs C++ or Jarva which needs dedication to get into.
~Sorcus |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 13 Feb 2013 02:39 PM |
> Are there any advantages to using tables over arrays in a language, or vice versa? With arrays, they limit you to a static data structure, and only allows one type of data type. If Lua uses tables, why do arrays exist in other languages?
Tables are Lua-specific. Arrays exist in most programming languages. Arrays are VERY fast compared to tables. |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 13 Feb 2013 02:46 PM |
| There's an paper called "The Implementation of Lua 5.0" which provides details on how Lua tables are implemented. I recommend reading it. |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 03:16 PM |
| Thanks, that was useful information. I think I'll start using numeric indices more often. |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 03:24 PM |
I'd like to bring up the opposite question. Why do most arrays start at 0? Shouldn't the first number be one?
Unrelated: Is zero even really a number? |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 03:38 PM |
| I'd like to bring up another discussion in this thread. This may seem nooby, but why can't you iterate through dictionaries? It ruined my whole game once I realized that. |
|
|
| Report Abuse |
|
|
| |
|
Sorcus
|
  |
 |
| Joined: 29 Nov 2010 |
| Total Posts: 3775 |
|
|
| 13 Feb 2013 03:48 PM |
Arceus, Seranok already answered your question on why they start at 0. The indices in C are offsets from the start of the memory where the array resides vs Lua where they are actually indices that you would normally think them to be (one is what is theory-wise the right start vs what is more intuitive for the end user)
~Sorcus |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 13 Feb 2013 03:52 PM |
Well, I don't think Seranok answered that specifically, but I quoted someone else's answer from the link Sorcus posted.
I believe 0 is a number. It might not have much of a value, but you need a number to represent nothing.
Which is what 0 does well. It's also essential for the "number" line and absolute values.
"If you want to become a Developer or Innovator for CSA, contact me." |
|
|
| Report Abuse |
|
|
lah30303
|
  |
| Joined: 15 Feb 2008 |
| Total Posts: 10027 |
|
|
| 13 Feb 2013 03:56 PM |
| I like how Java (I don't know other languages, so I'm just comparing with Java) allows you to use different things depending on what your array is going to be used for. In Java, arrays may be limiting, but you still have vectors which are somewhat similar to lua's tables, except in java you have to worry about object types which in lua you don't have to worry about, which is probably why they don't use tables (I'm guessing). |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 05:19 PM |
I hardly read this thread, so I decided to throw my hat in the ring.
In C++ I assume that arrays work based on memory addresses. As this code example shows: #include "iostream" //Roblox hates less-than and greater-than using namespace std; int main(){ int a[] = {8,4,2,5}; int ind = 2; cout(lessthansigntwice)(bool)(*(a+ind)==a[ind])(lessthansigntwice)endl; return 0; } //Output: 1 (or true)
Hopefully, you don't mind a lack of tabbing. From my perspective, starting from zero seems obvious because of how this code works. The index number is an offset from a starting address, if you're at the first value, there's no offset. Zero offset! It makes a lot of sense to think that one would be the first though. They don't shorten it to "1st" for nothing. |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 05:59 PM |
The main reason I find zero-based indexing is useful is for modular arithmetic.
If I have a hashing function that returns some random (positive) integer, but I have an array of fixed size N, a zero-based index allows me to just do
array[random_num % array.size] = ...
Whereas a 1-based system would be rand % size + 1, which is less elegant.
Some people claim that one-based is lame due to things related to looping through an array, but most languages these days have closures or syntactic sugar (i.e. Java's for-each) that completely avoid having to make a looping variable "i" at all, so I don't think that's a real point. |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 06:02 PM |
Well, first, you make the assumption that what JS and other languages do is "right" and "normal" while what Lua does is "wrong" and "odd". 0 indexing is a standard most likely because, as stated, arrays represent[ed] an essentially concatenated list of values, and the index could refer to offset. From an even simpler perspective, starting at 1 wastes one bit. If you are on a really old system and have small numbers in the order of 4 or 5 or 8 bits, then wasting the '0' is simply _wasteful_.
Of course, this is the perspective from the early development of computing, which is hardly an optimal or objective place to look from. Mathematics, for instance, often numbers indexes starting from 1, not 0, but really does use both. The usefulness of starting from 1 is that list[#length] is a valid entry and list[1] is the 1st entry, list[2] the second, list[n] the nth (as opposed to the [n+1]th).
Because Lua's tables aren't really arrays (they start at 0, and they have keys which are not just positive numbers) it isn't expedient to call them arrays--then you get into problems like this thread addresses with the usage being confused between languages. They really are "tables" of index-value pairs, rather than a simple ordered list.
So, TL;DR. Starting from 0 makes sense from a lowlevel computing perspective, but lacks [arguably] intuition and a certain basis in mathematics [the true model]. Lua "tables" are not called arrays because they aren't really "arrays" the way that C or old languages use them (other names are "list" , "conslist", "linked list", "vector", "tuple", "matrix", "map"). They have a different name to reflect that it is a different implementation. |
|
|
| Report Abuse |
|
|
|
| 13 Feb 2013 06:13 PM |
tables == lists lists ~= arrays
With lists (tables) you can add and remove values.
Arrays have a fixed length and data type.
` RIP Erik Cassel ` |
|
|
| Report Abuse |
|
|