tags:

views:

976

answers:

7

I'm at a stage where I am forced to learn Lua, so do you have any suggestions on how I do this? I don't have a lot of experience with any other scripting languages than PHP.

So, some suggestions on "head start lua"-pages?

EDIT

As an addition to the wonderful tutorial pages, could you plaese suggest any "programs" I could make that will help me learn Lua? Imagine I would want to learn Pointers in C++, I'd make a Linked List. I want to touch the bassics in LUA but meanwhile be open to pretty advance stuff.

+12  A: 

First of all work your way through the Programming in Lua, it should take you a day or two to get the gist of Lua.

However I can tell you right away on your first time through ignore coroutines and metatables, they are very powerful, but take a while to grasp. First learn the syntax, scoping (same as PHP luckily for you) and the standard libraries.

After that go back to coroutines and metatables, read them try them and by the third time through you might get it. Unless you have a very good CS background these are complex topics

Edit: The book is free online == website. Besides it is the best tutorial out there on Lua, everyone learns Lua with it.

Also: If you're purpose is Lua for World of Warcraft (probably not but just in case) you can check out this tutorial

And: Here is a tips and tricks thread on StackOverflow, might help give you some ideas of what to expect from Lua

Suggested Programs/Exercises:

Since you're initially looking at Lua for web development try to understand and improve the Data Description example in PIL. It'll give you a few good ideas and a nice feel for the power or Lua.

Then you might want to try out playing with the Data Structures chapter, although Lua has a single complex data-type, the Table, that chapter will show you Lua-like ways to make a table do anything you need.

Finally once you begin to grok metatables you should design a class system (yes with Lua you decide how your class system works). I'm sure everyone that knows Lua has made a dozen class systems, a good chapter to get you started on a class system is Object-Oriented Programming

And if you got time and know C or something like that (C# and Java included) try extending an application with Lua, but that'll take a week or two to do

Robert Gould
Thank you very much. I have something better than a CS-background, I have(soon) an degree in Software Engineering :)
Filip Ekberg
Yeah Software Engineering is better on the battle-field, but Metatables and Coroutines are the sort of thing CS people dwell about more than Engineers.
Robert Gould
Okay, thanks a lot mate! It's not for World of Warcraft, but when i know LUA, maybe i should try it out on a game. It's mainly for Web-stuff now.
Filip Ekberg
I'm at chapter 3.6 now, reading each row, it's really an awesome tutorual, even if i know what a float pointing number is, it's just so well written, looking forward to chapter 16! ;)
Filip Ekberg
good luck, and beware that Lua is a very attractive language, and fast too. Kind of like a Ferrari ;)
Robert Gould
Yeah I've been laughing through all the chapters just seeing my opportunities. Where have this been all my life eh? :)
Filip Ekberg
Note that, though the PiL book is online, it is the first edition, which is about Lua 5.0. There are some differences and changes that could confuse you. In any case, the book is well worth buying, it is very well written and a good reference.
Jan de Vos
Buying the books is a nice sign of support! But the online ones are good enough for starters. I rarely have had to reach out for the paperback when doing normal coding, but they are nice reads. The exception is the Modules chapter, which is very different, but that chapter is also luckily online.
Robert Gould
+2  A: 

In addition to the suggestions above, there's also the Lua wiki which is well worth a browse. There are a tremendous number of code snippets and small recipes there which can be useful.

Boojum
+1  A: 

You could install World of Warcraft and make a mod for that (it uses LUA). Actually that's probably a bad idea.

Maybe try to integrate LUA into a .NET application (assuming you are a C# programmer) and do something 'fun' with it:

Using LUA with C#

Or just browse lua.org

Chris S
Im gonna ignore the part about WoW ;) But the C#-part is lovely, thank you!
Filip Ekberg
+4  A: 

There is also a large body of projects related to Lua at LuaForge.

If you happen to use Windows as your day-to-day platform, then I would recommend getting the Lua for Windows package as a nice starting point. It includes a wide array of useful modules all prebuilt and installed together with the Lua interpreter.

After your first pass through PiL and the reference manual, you will want to read Lua Programming Gems which is currently only available in a paper edition.

<plug> Do consider buying the books through the associate links at Lua's books page or LuaForge to support the projects. </plug>

Edit: As for ideas for programming projects where Lua is suited, look for problems where the table provides leverage. Tables are central to Lua, since even the global variables are just fields in a table. Tables can be indexed by values of any data type except nil, but have an especially efficient implementation if used as arrays.

One quirk that trips up people coming from a C-like background is that all things in Lua are naturally indexed starting from 1. Strings are indexed from 1, arrays start at 1, etc. Don't worry about it too much, there is nothing wrong with using a[0], but the length of the array given by #a is defined assuming that the array began with a[1].

Another quirk is that functions don't really have names. They are first class values that are usually stored in some variable that has a name. Syntax sugar makes it look like they have names, but that is just a convenience.

Metatables are a particularly Lua-ish feature of tables (and other types, but that is a really advanced topic) that are the basis for most of the schemes for doing object-oriented things in Lua.

Closures and true tail calls are other features of Lua that aren't often found in small scripting languages that can really make some idioms easy to implement.

RBerteig
Awesome feedback! Thanks!
Filip Ekberg
+2  A: 

Funny to see all these elaborate lists (though they are certainly correct). Back in 2002, I read about the first 20+ pages of the Lua reference manual, and started using it. It really is that simple. Lua (and ANSI C) are of the few languages that really fit in one's mind all at once - and stay there. For the others, at least I need to constantly do some relearning.

Be aware that getting to think in Lua will take time. I think mine was 6 months or so. When coming from C/C++, we tend to solve problems in certain ways. Lua might offer better means (i.e. via use of tables) but it takes a while to start seeing those. This transition to a higher abstraction level is similar to the Assembler->C shift in the 1980's. Many people still coded a while in C as if it only were a portable assembler.

akauppi
+1  A: 

I wrote a short quick-start guide to Lua for people using it on a project I was working on. If you are familiar with other scripting languages it may get you up and running quickly. The docs on Lua.org are very good and should cover most everything else you need. Lua is a pretty small language and can be learned fairly quickly.

Nick
+1  A: 

This is a pretty general recommendation, but if you want to get started in a new programming language as a software engineer, it's fun to start doing the problems found at Project Euler in your new programming language. I've been doing this with Python recently and found it to be inspiring and bring a lot of enthusiasm to the coding.

Stefan Thyberg
Amazing site! Thanks!
Filip Ekberg