Languages

Build-In Support

EnlighterJS v2.11.1 is out including new Languages & Themes. View the Changelog

LUA

Select Theme

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor local a = "Double Quoted\nString" invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.

-- SOURCE: http://www.lua.org/manual/5.3/manual.html

-- COMMENTS
-- --------------------------------------------------

-- This is a single line comment
print "Hello World!"

--[[Comments can be spread
across several lines ]]
print "Hello World!"

---[[The long handled doubleshovel means that this code will run
print "This will print because it is not a comment!"
-- We can still include comments by prefixing them with a doubledash
-- print "This will not print because it is commented out"
]]

-- STRINGS
-- --------------------------------------------------
local a = "Double Quoted\nString"
local b = 'Double Quoted\nString'
local c = [[Multi
Line String
    ]]

a = 'alo\n123"'
a = "alo\n123\""
a = '\97lo\10\04923"'
a = [[alo
123"]]
a = [==[
alo
123"]==]

-- NUMBERS
-- --------------------------------------------------
a = 4
b = 0.4
c = 4.57e-3
D = 0.3e12
e = 5e+20

-- CONSTANT
-- --------------------------------------------------
a = true
b = false
c = nil
instance.merge(true, false, nil, 1234, 871.124E-12)

-- LOGICAL OPERATORS
-- --------------------------------------------------
print(4 and 5)         --> 5
print(nil and 13)      --> nil
print(false and 13)    --> false
print(4 or 5)          --> 4
print(false or 5)      --> 5
max = (x > y) and x or y
print(not nil)      --> true
print(not false)    --> true
print(not 0)        --> false
print(not not nil)  --> false

-- TABLES
-- --------------------------------------------------
a = {}     -- create a table and store its reference in `a'
k = "x"
a[k] = 10        -- new entry, with key="x" and value=10
a[20] = "great"  -- new entry, with key=20 and value="great"
print(a["x"])    --> 10
k = 20
print(a[k])      --> "great"
a["x"] = a["x"] + 1     -- increments entry "x"
print(a["x"])    --> 11

days = {"Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"}

-- EXAMPLE
-- --------------------------------------------------
line = io.read()     -- read a line
n = tonumber(line)   -- try to convert it to a number
if n == nil then
  error(line .. " is not a valid number")
else
  print(n*2)
end

function foo (a)
print("foo", a)
return coroutine.yield(2*a)
end

co = coroutine.create(function (a,b)
   print("co-body", a, b)
   local r = foo(a+1)
   print("co-body", r)
   local r, s = coroutine.yield(a+b, a-b)
   print("co-body", r, s)
   return b, "end"
end)

print("main", coroutine.resume(co, 1, 10))
print("main", coroutine.resume(co, "r"))
print("main", coroutine.resume(co, "x", "y"))
print("main", coroutine.resume(co, "x", "y"))

list = nil
    for line in io.lines() do
      list = {next=list, value=line}
    end

x = 10
local i = 1        -- local to the chunk

while i<=x do
  local x = i*2    -- local to the while body
  print(x)         --> 2, 4, 6, 8, ...
  i = i + 1
end

if i > 20 then
  local x          -- local to the "then" body
  x = 20
  print(x + 2)
else
  print(x)         --> 10  (the global one)
end

print(x)           --> 10  (the global one)



do
  local a2 = 2*a
  local d = sqrt(b^2 - 4*a*c)
  x1 = (-b + d)/a2
  x2 = (-b - d)/a2
end          -- scope of `a2' and `d' ends here
print(x1, x2)