Window = {}
Window.prototype = {x = 0, y = 0, width = 100, height = 100}
Window.mt = {}
function Window.new(o)
setmetatable(o,Window.mt)
return o
end
Window.mt.__index = Window.prototype
w = Window.new({x = 10, y= 20})
z = Window.new{height = 100, width = 50}
w.width
z.x
Building = {lat = 39.2555, long = 76.7113}
function Building.print(self)
print("I'm located at " .. self.lat .. ", " .. self.long)
end
Building.__index = Building
UMBC = {}
setmetatable(UMBC,Building)
UMBC.print(UMBC)
House = {numberOfBathrooms = 35}
function House.printBaths(self)
print("I have " .. self.numberOfBathrooms .. " bathrooms.")
end
setmetatable(House,Building)
T = {}
T.__index = House
whiteHouse = {lat = 38.8977, long = 77.0336}
setmetatable(whiteHouse,T)--{__index = House})
whiteHouse.print(whiteHouse)
whiteHouse.printBaths(whiteHouse)
--Based off Fabio Mascarenhas' example
mt = {}
function mt.__add(c1,c2)
if(getmetatable(c1) ~= mt) then
res = {r = c1 + c2.r , i = c2.i}
setmetatable(res,mt)
return res
end
if(getmetatable(c2) ~= mt) then
res = {r = c1.r + c2, i = c1.i}
setmetatable(res,mt)
return res
end
res = {r = c1.r + c2.r , i = c1.i + c2.i}
setmetatable(res,mt)
return res
end
complex = {r = 1, i = 2}
complex2 = {r = 2, i = 4}
setmetatable(complex,mt)
setmetatable(complex2,mt)
print(5 + complex)
print(5 - complex)
Animal = {species = 'Dog', noise = "woof"}
function Animal.makeNoise(animal)
print(animal.species .. " goes " .. animal.noise)
end
Animal.makeNoise(Animal)
Animal2 = {species = "Dog" , noise = "woof"}
function Animal2:makeNoise(punct)
print( self.species .. " goes " .. self.noise .. punct )
end
Animal2:makeNoise("!")
Animal2.makeNoise(Animal2,"!")
Animal3 = {species = "Dog" , noise = "woof"}
function Animal3.makeNoise(self)
print( self.species .. " goes " .. self.noise )
end
Animal3:makeNoise()
type(Animal3)
Window = {}
Window.prototype = {x = 0, y = 0, width = 100, height = 100}
Window.mt = {}
Window.mt2 = {}
function Window.new(o)
setmetatable(o,Window.mt)
return o
end
Window.mt.__index = Window.prototype
Window.mt2.__index = {color = "red", shape = "circle"}
x = Window.new{height = 200}
print(x.x,x.y,x.width,x.height)
setmetatable(x,Window.mt2)
print(x.x,x.y,x.width,x.height)
x = {10, 20, 30, 40}
print(x[1])
words = {}
f = io.open("words.txt")
for line in f:lines() do
for word in string.gmatch(line,"%w+") do
table.insert(words,string.lower(word))
--print(word)
end
end
counts = {}
for _,word in pairs(words) do
counts[word] = 0
--print(word)
end
for _,word in pairs(words) do
counts[word] = counts[word] + 1
--print(word)
end
together = {}
for i,w in pairs(counts) do
table.insert(together,{word = i, count = w})
end
table.sort(together,function(a,b) return a.count > b.count end)
for i,w in ipairs(together) do
print(w.word, w.count)
end