
Mudlet variables mod#
This works because mod here = M in mod.lua: It's like mod.lua is a function body, so that - locals inside mod.lua are invisible outside it. require acts like: (if not cached see below) local mod = ( function () require is the standard way to include modules. Another file can use mod.lua's functionality: local mod = require( 'mod') - Run the file mod.lua. Local function sayMyName () print( 'Hrunkner')Įnd function M.sayHello () print( 'Why hello there') String keys can use js-like dot notation: print(t.key1) - Prints 'value1'. Using tables as dictionaries / maps: - Dict literals have string keys by default:

Similar to php arrays or js objects, they are - hash-lookup dicts that can also be used as lists. Tables = Lua's only compound data structure - they are associative arrays. Calls with one string param don't need parens: print 'hello' - Works fine. Trig funcs work in radians, by the way. These are the same: function f (x) return x * x endį = function (x) return x * x end - And so are these: local function g (x) return math.sin(x) end local g g = function (x) return math.sin(x) end - the 'local g' decl makes g-self-references ok. Functions are first-class, may be local/global. X, y = bar( 'zaphod') -> prints "zaphod nil nil" - Now x = 4, y = 8, values 15.42 are discarded.

Unmatched receivers are nil - unmatched senders are discarded. function fib (n) if n 25 print(a2( 64)) -> 100 - Returns, func calls, and assignments all work - with lists that may be mismatched in length. Another loop construct: repeat print( 'the way of the future') KarlSum = 0 for i = 1, 100 do - The range includes both ends.Įnd - Use "100, 1, -1" as the range to count down:įredSum = 0 for j = 100, 1, - 1 do fredSum = fredSum + j end - In general, the range is begin, end. This is similar to the a?b:c operator in C/js:Īns = aBoolValue and 'yes' or 'no' -> 'no' This is not an error:įoo = anUnknownVariable - Now foo = nil.ĪBoolValue = false - Only nil and false are falsy 0 and '' are true! if not aBoolValue then print( 'twas false') end - 'or' and 'and' are short-circuited. line)Įnd - Undefined variables return nil.
Mudlet variables how to#
How to make a variable local: local line = io.read() - Reads next stdin line. io.write( 'not over 40\n') - Defaults to stdout.

Equality check is = like Python ok for strs. Don't freak out, 64-bit doubles have 52 bits for - storing exact int values machine precision is - not a problem for ints that need 40 then print( 'over 40')Įlseif s ~= 'walternate' then - ~= is not equals. I found an excellent Lua primer here which is reproduced with kind permission from Tyler Neylon below.
