Luatutorial
Luatutorial
print("Hello World")
--[[
Multiline comment
]]
-- Variable names can't start with a number, but can contain letters, numbers
-- and underscores
-- You can store any data type in a variable even after initialization
name = 4
io.write("My name is ", name, "\n")
-- Lua only has floating point numbers and this is the max number
bigNum = 9223372036854775807 + 1
io.write("Big Number ", bigNum, "\n")
-- Every variable gets the value of nil by default meaning it has no value
io.write(type(madeUpVar), "\n")
age = 13
if (age < 14) or (age > 67) then io.write("You shouldn't work\n") end
-- Either surround the number with quotes, or convert the string into
-- a number
until tonumber(guess) == 15
print()
-- Cycle through table where k is the key and v the value of each item
for k, v in pairs(months) do
io.write(v, " ")
end
print()
-- Items in Table
io.write("Number of Items : ", #aTable, "\n")
for i = 0, 9 do
aMultiTable[i] = {}
for j = 0, 9 do
aMultiTable[i][j] = tostring(i) .. tostring(j)
end
end
function splitStr(theString)
stringTable = {}
local i = 1
-- Cycle through the String and store anything except for spaces
-- in the table
for str in string.gmatch(theString, "[^%s]+") do
stringTable[i] = str
i = i + 1
end
for j = 1, numOfStr do
print(string.format("%d : %s", j, splitStrTable[j]))
end
for k, v in pairs{...} do
sum = sum + v
end
return sum
end
-- Call for it to run with resume during which the status changes to running
coroutine.resume(co)
co2 = coroutine.create(function()
for i = 101, 110, 1 do
print(i)
end end)
coroutine.resume(co2)
coroutine.resume(co)
file:seek("set", 0)
print(file:read("*a"))
file:close()
mt = {
-- Define how table values should be added
-- You can also define _sub, _mul, _div, _mod, _concat (..)
__add = function (table1, table2)
sumTable = {}
for y = 1, #table1 do
if (table1[y] ~= nil) and (table2[y] ~= nil) then
sumTable[y] = table1[y] + table2[y]
else
sumTable[y] = 0
end
end
return sumTable
end,
addTable = {}
setmetatable({}, Animal)
return self
end
animalStr = string.format("%s weighs %.1f lbs, is %.1f in tall and says %s",
self.name, self.weight, self.height, self.sound)
return animalStr
end
-- Create an Animal
spot = Animal:new(10, 15, "Spot", "Roof")
Cat = Animal:new()
return self
end
catStr = string.format("%s weighs %.1f lbs, is %.1f in tall, says %s and loves
%s", self.name, self.weight, self.height, self.sound, self.favFood)
return catStr
end
-- Create a Cat
fluffy = Cat:new(10, 15, "Fluffy", "Meow", "Tuna")
print(fluffy:toString())