Skip to content

Instantly share code, notes, and snippets.

@jwamin
Created May 6, 2023 15:51
Show Gist options
  • Save jwamin/c52b0b969314299c98066a10b9fb8c7b to your computer and use it in GitHub Desktop.
Save jwamin/c52b0b969314299c98066a10b9fb8c7b to your computer and use it in GitHub Desktop.
lua tutorial sandbox
#!/usr/bin/lua
-- LUA
--
-- Tutorial https://www.tutorialspoint.com/lua/lua_standard_libraries.htm
-- Resources https://github.com/LewisJEllis/awesome-lua - resources
-- Packages https://luarocks.org/
print("hello world")
-- this is a comment
mad = "mad"
mad = string.upper(mad)
print("It's a very very... \a"..mad..[[ world ]])--[[inline comment .. concats two strings --]] --"comment"
print(type("What is my type")) --> string
t = 10
arr = {
5.8*t, --> number
true, --> boolean
print, --> function
nil, --> nil
type(ABC), --> string
}
function typePrinter(variable_for_printing)
print(type(variable_for_printing))
end
for key, value in ipairs(arr) do
typePrinter(value)
end
lua = "Lua"
print(string.byte(lua))
print(string.len(lua))
-- Tables
local myTable = {}
myTable[1] = "LuaTable"
myTable[2] = function(arg)
print(arg)
end
myTable[2](myTable[1])
myTable = nil
if myTable then
myTable[2](myTable[1])
else
print("guess not")
end
-- Modules and metatables
mod = require("mymod")
print("mymodule")
mod.myModuleFunction()
setmetatable(mod,{keyZ="metatable value"})
print(getmetatable(mod)["keyZ"])
-- coroutines
c = coroutine.create(function (seconds,max)
assert(seconds > 0, "you gotta wait for longer than 0 seconds- sorry")
for i = 1, max do
mod.fnwithwait(seconds)
print("coroutine: "..tostring(coroutine.running()).." waited for "..seconds..", will run another "..max-i.." times")
coroutine.yield()
end
print("end")
return 5
end)
time = 1
max = 1
for i = 0, max do
coroutine.resume(c,time,max)
print(r,c,"isrunning: "..tostring(coroutine.running()))
print("status: "..coroutine.status(c))
--assert(!(coroutine.status == dead) && i < max,"coroutine did not complete")
end
-- Files
f = io.open("test.json", "r")
io.input(f)
all = f:read("*a")
io.close(f)
--cjson //from luarocks
local cjson = require("cjson")
obj = cjson.decode(all)
print(obj,"value: "..obj["hello"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment