Anyone who plays World of Warcraft and does even the slightest of development knows that Lua is an integral part of its core. It is so important in fact, that Lua can be interpreted from right inside the game itself. Now, besides WoW, Lua is used throughout the gaming world in a host of ways, typically for the UI. This makes Lua something some of us would like to learn about. Today, we are going to take the first step in Lua development and going over the powerful table.
Recently, I began a class in "Game Scripting", which includes Lua scripting. One of the most useful features I have learned so far is the use of an object known simply as a table. This object has a lot that it can do, so let's get started. Also, it is good to note that Lua is 1 based and not 0 based.
First and foremost, a table can be your regular, run-of-the-mill array. But in this sense, it is a completely dynamic array, allowing you to add elements on the fly. Tables are defined simply by using curly brackets:
print(myTable[3]) --output: a string
As you can see, you can put just about anything you want inside of a table. A string, an int, or even a float. It doesn't matter in Lua, what you want inside of it, you can stuff it in there. It is a very forgiving language. And if you create a simple array this way, it also happens to start with 1 as the first index. But it gets even better than this.
Once created, you can add an element by simply referencing an index that is not yet there. The funny thing is, it can be pretty much any index you want. Lets take our table and add something ridiculous to it:
print(myTable[54343]) --new string
It is quite crazy to have an array that large, but the point being here that the index can be any number you pretty much want. As long as you don't overwrite an element you already have, you have added a new element. So, as an alternative to populating our table at initialization, we can do something like so:
myTable[1] = 1
myTable[2] = "string"
myTable[3] = 2.32
It is not as efficient as the original method, but it is completely possible to do something like that. Again, you can put anything you want inside the elements. Lua doesn't really care, it can handle it all.
Now, besides the dynamic aspect of tables, tables can also be associative arrays. You can add properties that label the data, creating a simply structure. For example:
name = "SOTC",
awesomeness = 100000
}
print(myTable.name) --SOTC
So here we have an incredibly simple table object which is acting pretty much like an associative array. But in order to access the data, we use the dot operator, just like objects in C# or C++. Slightly strange for those of us used to that sort of thing, but it makes it really easy to use tables. However, if it suits you, you can use the old fashion way as well:
This way works just fine as well. Either way will get you the data from that element. Another crazy thing about tables is that you can mix and match normal elements with associative ones, so you could have a table that was defined like this:
aString = "charcter string"
aFloat = 2.32,
4, 5, 6
}
So, you can have elements related to a key or not, it is all up to you. This makes tables extremely versatile. But how about adding a new key/value pair? Just like adding new elements via index, a new associated element can be anything you want, just make sure you pass a string as the index instead:
myTable["aString"] = "Some Text"
That is how simple it is. Lua is very easy to deal with because it is very forgiving. You can add elements all you want. But what about removing elements? To remove an element, you have to use a method of the actual table class. The remove method will remove an element at a specified index and shift the other elements down. This will only work for indexed items, not keyed items (value with string key). You can also set the value of a index or key to nil. Below we remove the value at position 1 and set "aString" to nil. The last line actually will loop through the elements and print them out.
myTable["aString"] = "Some Text"
table.remove(myTable, 1)
myTable.aString = nil
for key,value in pairs(myTable) do print(key,value) end
-- key value
-- 1 2
-- 2 3
-- 3 4
This wraps up this tutorial on the basics of a Lua table. Now you know how to create and do some simple modifications. This is not all you can do with the wondrous table, but there will be more to come in the next tutorial. Just remember, when you need coding help, all you have to do is Swtich On The Code.
09/18/2009 - 22:53
Every index a Lua table is a key/value pair. There is syntactic sugar and implementation details for using one as an array, but there really is no difference. That is also one of the most confusing aspects of Lua, which is generally quite intuitive.
The sugar (these result in identical tables):
t1 = {1, 2, 'a', 'b'}
t2 = {[1] = 1, [2] = 2, [3] = 'a', [4] = 'b'}
The table.remove function also doesn't do quite what you implied and, as a result, your last example does absolutely nothing. If you want to remove a key from a table, simply set it to nil.
09/18/2009 - 23:08
The end of the post has been updated so this is no longer the case. However, table.remove is a completely valid function. You can check out the official documentation over at lua.org.
09/23/2009 - 01:59
Hi!
Congratulations! Your readers have submitted and voted for your blog at The Daily Reviewer. We compiled an exclusive list of the Top 100 Programming Blogs, and we are glad to let you know that your blog was included! You can see it at http://thedailyreviewer.com/top/programming/2
You can claim your Top 100 Blogs Award here : http://thedailyreviewer.com/pages/badges/programming
P.S. This is a one-time notice to let you know your blog was included in one of our Top 100 Blog categories. You might get notices if you are listed in two or more categories.
P.P.S. If for some reason you want your blog removed from our list, just send an email to angelina@thedailyreviewer.com with the subject line "REMOVE" and the link to your blog in the body of the message.
Cheers!
Angelina Mizaki
Selection Committee President
The Daily Reviewer
http://thedailyreviewer.com
10/31/2009 - 12:00
I think you mean "associative array," not "associated array." If your arrays are associated, you may be looking at a Skynet situation.
11/02/2009 - 10:47
Yep, nice catch. Updating now.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.