Skip to content

Instantly share code, notes, and snippets.

@lduboeuf
Created February 1, 2017 09:53
Show Gist options
  • Save lduboeuf/13a8f75b1701e9204c1202d9bab7a482 to your computer and use it in GitHub Desktop.
Save lduboeuf/13a8f75b1701e9204c1202d9bab7a482 to your computer and use it in GitHub Desktop.
lua first step 2
local firstname = "lionel"
local display = print
display(firstname)
--table as associative array
local person = {
firstname = "lilil",
developer = true,
score = 150000,
loose = function(self, nb)
self.score = self.score - nb
end
}
person.sayHello = function()
display("hello "..person.firstname.." score: "..person.score)
end
--syntactic sugar for person.loose(person, 125000)
person:loose(125000)
person.sayHello()
@lduboeuf
Copy link
Author

lduboeuf commented Feb 1, 2017

--unidimensional array
local a = {7,12,4}
--iterate
for idx, value in ipairs(a) do
  print(idx, value)
end

--a function can return several values
local function connect()
  --simulate error
  return nil, "unable to connect to..."
end

local db, err = connect()
if not db then
  print("oups..."..err)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment