Lua Notes
default
#!/usr/bin/env lua
-- leftoff: pil 28
print()
print [[
-------------------
---- Lua Notes ----
-------------------
]]
-- Lua is case sensitive.
print("Yes, lua is case sensitive\n")
print "[[ These brackets are used to print multilines ]]"
print()
print ([[See I am multiple lines long,
without the brackets lua would
cry about it.]], "\n")
-- This is a comment; till end of line.
print("-- Makes a comment till the end of the line\n")
--[[
This is a block comment
]]
print ("--[[ Block comments can be useful,")
print ("and lua knows it! ]]\n")
--[[
Block comments can also be terminated like this:
--]]
print("--[[ There is more more than one way to")
print "terminate a block comment.."
print "and this way has a little trick.--]]"
print()
---[[ The third dash turns this into a line comment only
print("Oh no I'm not hidden from you anymore!\n")
--]]
---[[
print([[Adding an extra dash, undoes the blocking
of a block comment, and turns it into a line
comment.]])
print()
print("---[[I am no longer a block comment --]]")
print([[So code inside a block can be renabled easily.
and disabled; beneficial for testing!]])
print()
print("Use --]] to close these testing blocks")
print("Since it becomes a line comment also")
print()
--]]
print("Who's your mammy?\n")
print [[
lua whoyomammy.lua
-- Run a file
]]
print [[
----------------------
-- Interactive Mode --
----------------------
lua
-- Runs the interpreter.
-- It's source is: lua.c
lua -i chosen-chunk.lua
-- will run a chunk of code and jump into interpreter
-- "-i" interactive
lua [options] [script (args)]
-- Usage of lua, from command line.
lua -e "print(1 + 2 + 3)"
lua -e print\(1+2+3\)
-- "-e" lets you run code directly from cmdline.
lua -i -lfake_lib -e "x = 12; y = 15; z = 20;"
-- This would:
-- Load the lib library.
-- "-l" load file (require) [similar to dofile()]
-- Assign the x, y, and z.
-- Put the user in interactive mode. "-i"
lua -i -e "x = 12; y = 15; z = 20;" fake_lib.lua
-- When loading a script the interpreter can
-- access it's name and arguments in the arg
-- variable. In this case:
= arg[1] -- nil
= arg[0] -- fake_lib.lua
= arg[-1] -- x = 12; y = 15; z = 20;
= arg[-2] -- -e
= arg[-3] -- -i
= arg[-4] -- lua
= 4 + 19
= "My mammmy and me"
return "Bye bye!"
print "Come back to me?"
print(1 + 2 + 3)
-- In interactive mode "=" to print a value
-- of an expression
-- = is short for return.
-- end-interactive-mode --
--------------------------
]]
----------------------
-- chosen-chunk.lua --
----------------------
function thrice (x)
return 3*x
end
print ("You can now use function thrice.")
print ("Usage: print(thrice(9))\n")
print ("Thrice of 9 is:", thrice(9), "\n")
--
-- end-chosen-chunk.lua
-----------------------
print [[
dofile("chosen-chunk.lua")
-- Load a file, useful in interactive mode
]]
print [[
os.exit()
ctrl-d -unix
ctrl-z -windows
-- Exits lua interactive interpreter
-- ^d and ^z are end of file control characters
]]
print [[
------------------
-- Script Files --
------------------
#!/usr/bin/lua
#!/usr/local/bin/lua
-- You can run it like a normal unix script with
-- the path to your lua as the first shebang line.
#!/usr/bin/env lua
-- This lets the environment choose which lua
-- to run. It is more portable than the two
-- above.
]]
print [[
--------------------
-- Reserved Words --
--------------------
and break do else elseif
end false goto for function
if in local nil not
or repeat return then true
until while
]]
print [[
-----------------
-- Basic Types --
-----------------
nil boolean number string
userdata function thread table
-- You can use set a variable to nil to delete
-- it. It will end up being garbage collected.
-- nil is also the only value other than false
-- that is considered false in a conditional.
You can get the type with the fuction:
type
]]
print [[ print(type("Some type defs:")) ]]
print(type("Some type defs:"))
print()
print [[ print(type(10+4*2)) ]]
print(type(10+4*2))
print()
print [[ print(type(type)) ]]
print(type(type))
print()
print [[ print(type(false)) ]]
print(type(false))
print()
print [[ print(type(nil)) ]]
print(type(nil))
print()
print [[ print(type(type(ABC123XYZGIVEASTRINGTOME))) ]]
print(type(type(ABC123XYZGIVEASTRINGTOME)))
print()
print [[ print(type(fruits)) ]]
print(type(fruits))
print()
print [[
fruits = 'kiwi'
print(type(fruits)) ]]
fruits = 'kiwi'
print(type(fruits))
print()
print [[
fruits = 5
print(type(fruits)) ]]
fruits = 5
print(type(fruits))
print()
print [[
-------------
-- Numbers --
-------------
6 6.7 6.66e-6 0.6e8 6E+30
]]
print(6)
print(6.7)
print(6.66e-6)
print(0.6e8)
print(6E+30)
print()
print [[
----------------------
-- The Little Hexer --
----------------------]]
print [[print (0xBEEfACE)]]
print (0xBEEfACE)
print ()
print [[print (0xdeafBABE)]]
print (0xdeafBABE)
print ()
print [[print (0xBADcafe)]]
print (0xBADcafe)
print ()
print [[print (0xAdaFade.95)]]
print (0xAdaFade.95)
print ()
print [[print (0x3cd0p-1)]]
print (0x3cd0p-1)
print ()
print [[print (0xb.eep2)]]
print (0xb.eep2)
print ()
print [['\x50\x6c\x65\x61\x73\x65\x20\x71\x75\x69\x74' ..
'\x20\x68\x65\x78\x69\x6e\x67\x20\x6d\x65\x21\x0a']]
print('\x50\x6c\x65\x61\x73\x65\x20\x71\x75\x69\x74' ..
'\x20\x68\x65\x78\x69\x6e\x67\x20\x6d\x65\x21\x0a')
-- Inside [[ ]] escape sequences are ignored
print [[ '\070\105\110\101\032\073\039\108\108\032' ..
'\100\101\099\105\109\097\108\032\121\111\117\046\046\046\010']]
print('\070\105\110\101\032\073\039\108\108\032' ..
'\100\101\099\105\109\097\108\032\121\111\117\046\046\046\010')
print [[
-- ".." Concats a string (this happens at runtime)
-- "\z" Concats at compile time and strips extra whitespace (lua 5.2+)
]]
---[[
print('I am so short, \z
could you make me longer, \z
so I can be confident?')
--]]
print [[
-- Needs lua5.2 or above to run with \z
-- Remove the third hyphen to see me expand,
-- as long as your lua is not too young.
]]
print [[
-------------
-- Strings --
-------------
]]
print("gsub that intern:")
intern = "helpless"
new_intern = string.gsub(intern, "less", "ful")
print(new_intern)
print (intern)
print()
ariana = "favorite girlfriend"
print (ariana)
ariana = string.gsub(ariana, "favorite ", "horrible ex-")
print(ariana)
print()
print("# is the length operator")
print(#new_intern)
print(#"too kinky, quit counting my characters...#")
print()
print ("Escaping:")
print ("get off my line!\n\"don't quote me!\", 'got it?'")
print ('I\'ll backhand you! \'\\smack\\\'')
print("'\\smack\\' again")
print()
print("\" \" are no different than ' '")
print('Single quotes are no cooler that double quotes in lua')
print('At least they let you sway away from "escaping" \'stuff\'')
print()
print [[
----------------------
-- Escape Sequences --
----------------------
\a bell
\b backspace
\c form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\" double quote
\' single quote
]]
print [[
--end-basic-types
-----------------
]]
print [[
-------------------------------------
-- Variables / Names / Identifiers --
-------------------------------------
Names can be made of: strings, digits, underscores.
**Names should not start with an undercore plus UPPERCASE.
_UPPER_VARS are reserved for special uses. _NOGOOD _allgood.
**You can not use "-" in variable names.
** my-var is not valid.
]]
print [[
-- Ways they can be set:
]]
boysCount = 1
girlsCount = 2;
manCount = 2 womanCount = 1
earthlingCount = 100; ghostlyCount = 1000
apple, golden_apple = "evil", "dispute"
print [[
-- ";" After a statement is not required, but can
-- be used for better readability.
]]
print [[
----------------------
-- Global Variables --
----------------------
-- Variables do not need to be declared. Undeclared
-- they will have the value of: nil.
-- All variables are global by default, unless declared
-- local.
]]
print "undeclared_variables are:"
print(undeclared_variable)
-- prints nil
print()
declared_variable = "I declare myself!"
print [[declared_variable = "I declare myself!"]]
print(declared_variable)
-- Assign a variable and print it.
print()
print [[
---------------------------------------
-- Local Variable vs Global Variable --
---------------------------------------
-- Varaibles inside functions and other such
-- blocks, will need to be declared local
-- if it is desired.
]]
print [[
function foo()
local agoraphobia = "I don't want to..."
-- Set a local variable
end
]]
print("Agoraphobia want to go to a party?")
agoraphobia = "I ain't going outside!"
-- Set a global variable
print(agoraphobia, "= Global by default")
function foo()
local agoraphobia = "I don't want to..."
-- Set a local variable
print(agoraphobia, "= Local to foo")
-- Print variable local to foo
end
foo()
-- Call function foo printing it's local variable
print(agoraphobia, "= From the globe")
-- Print the global
function bar()
agoraphobia = "I said, no leave home!"
-- Set the global
print(agoraphobia, "= Global set and printed by bar function")
end
bar()
print(agoraphobia, "= Yeah yeah global")
-- Prints the global changed by bar
--end-variables
---------------
print [[
---------------
-- Functions --
---------------
]]
-- factorial function
function factorial(n)
if n == 0 then
return 1
else
return n * factorial(n-1)
end
end
print ("You can now use function factorial.")
print ("Usage: print(factorial(9))\n")
print ("Factorial of 9 is:", factorial(9))
print()
-- end-functions --
-------------------
--[[
------------------------------------------
-- ** Remove interactive from this page **
-- Interactive Code
-- factorial function
function fact(n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end
print("Enter a number to factorial:")
a = io.read("*n") -- reads a number
print(fact(a))
-- end of factorial example
--end-interactive-code
--** **
----------------------
--]]
default