Table of Contents

Introduction to Lua


Lua


Lua Online Resources


Lua Execution


Lua Syntax

Data Types

Variables and Identifiers

Global and local variables

No integer types

Rename anything

                x = io
                x.read()
                io = "Hello world!"
                x = "Let's make io uncallable!"
                io.read()

Operators and Assignment

Concatenate strings with ..

Logical not operators

Strings and integers

Lua and OOP

              -- This is a Lua comment.
              -- Definition of the Earth "class"
              -- starts with a table called Earth:
              Earth = {martians = 5389}
              function Earth:casualties (survivors)
                 Earth.martians = Earth.martians - survivors
                 print("Earth is free! "..Earth.martians.." martians survived!")
              end


              -- Call the Earth:casualties method:
              Earth:casualties(5380)
              -- The casualties function is part of the table this time
              -- so that it can be called via the dot or the colon syntax.
              Earth = {martians = 5389,
                 casualties = function (self, survivors)
                    self.martians = self.martians - survivors
                    print("Earth is free! "..self.martians.." martians survived!")
                 end
              }


              Earth.casualties(Earth, 5380)
              Earth.martians = 5389
              Earth:casualties(5380)

Lua vs. Python

print("What's your name?")
name = io.read()
questions = {
  ["Which came first? Minix or Unix?"] = "Unix", 
  ["Who created Linux?"] = "Linus Torvalds",
  ["In what year was Linux created?"] = "1991"
}

correct_answers = 0
for key,value in pairs(questions) do
   print(key)
   answer = io.read()
   if answer == value then
     correct_answers = correct_answers + 1
   end
end

if correct_answers == 0 then
   print("You need to browse Wikipedia!")
else
   print("\nGood job, "..name.."!")
   print("Correct answers: "..correct_answers..")
end

* Equivalent Python code

name = raw_input("What's your name?\n")
questions = {"Which came first? Minix or Unix?":"Unix",
"Who created Linux?":"Linus Torvalds",
"In what year was Linux created?":"1991"}
correct_answers = 0

for key in questions:
   print key
   answer = raw_input()
   if answer == questions[key]:
      correct_answers += 1
	
if correct_answers == 0:
   print "You need to browse Wikipedia!"
else:
   print "\nGood job, " + name + "!"
   print "Correct answers: ", correct_answers

Lua Tables

See the Tables Tutorial at lua-users.org.

For an example of a two-dimensional array implemented through tables, see the LOVE game framework Gridlocked Player example.


Patterns (Regular Expressions)

See the Patterns Tutorial at lua-users.org.


Performance

See some notes on Lua performance at trac.caspring.org


Coroutines (Threading)

See the Coroutines Tutorial at lua-users.org.