# Lua Introduction

### by "Blag" - Senior Developer Evangelist ## Who uses Lua?

• Adobe -> Photoshop Lightroom, uses Lua for its user interface.
• Artweaver -> Scripting filters.
• Cisco -> Dynamic Access Policies within the Adaptive Security Appliance.
• MySQL -> MySQL Workbench extensions and add-ons.
• Blizzard -> Extensions for World of Warcraft.
• Mercedes-Benz -> Not specified.

## Basic Concepts

### Precedence must be enforced by parenthesis

```				```
print(2*3+5)

11

print(2*(3+5))

16
``````

## Basic Concepts

### Printing on the screen is easy...

```				```
text = "This is Lua!"

print(text)

This is Lua!
``````

### Variables can have multi-assignments...

```				```
a, b, c = 1, 2, 3

print(a,b,c)

1     2     3
``````

## Data Structures

### The only data structure in Lua is called "Table"...

```				```
tab = {}

table.insert(tab, {Name="Blag", Age=37})

for _,tab in pairs(tab) do

print (tab.Name .. " ==> " .. tab.Age)

end

Blag ==> 37
``````

```				```
tab = {3,1,4,2,5}

table.sort(t)

for _,t in pairs(tab) do

print(t)

end

1

2

3

4

5
``````

### Tables are flexible enough to represent other data structures...

```				```
intArray = {1, 2, 3}

intArray = {}

for i=1,3 do

intArray[i] = i

end

stringArray = {"This","is","Lua!"}
``````

### Multidimension Arrays are easy too...

```				```
multiArray = {}

for i=1,2 do

multiArray[i] = {}  --Creates a new row

for j=1,3 do

multiArray[i][j] = 0

end

end
``````

### Another way...

```				```
multiArray = {}

for i=1,2 do

for j=1,3 do

multiArray[i * 3 + j] = 0

end

end
``````

### Dictionary

```				```
myDict = {one = 1, two = 2}

print(myDict.one)

1

myDict.three = 3

myDict.two = nil

for x in pairs(myDict) do

print (myDict[x])

end

3

1
``````

## Functions

### Functions must always return a value...

```				```
function Hello(name)

return ("Hello " .. name)

end

print(Hello("Blag"))

Hello Blag

``````

### But they can return more than one value...

```				```
function Hello(name)

return ("Hello " .. name),

("and Goodbye " .. name)

end

a, b = Hello("Blag")

print(a, b)

Hello Blag     and Goodbye Blag
``````

## Fibonacci List

### Finally...we're going to make our first application...

```				```
function fib(num,a,b)

local result = ""

if a > 0 and num > 1 then

result = result .. (a+b) .. " " ..

fib(num-1,a+b,a)

elseif a == 0 then

result = a .. " " .. b .. " " ..

(a+b) .. " " .. fib(num-1,a+b,b)

end

return result

end
``````
```				```
io.write("Enter a number: ")

print(fib(num,0,1))
``````

## Making an LED Number App

### This is one of my favorite codes of all time...

```				```
local function split(s,delim)

local result = {}

for match in (s..delim):gmatch("(.-)"..delim) do

table.insert(result,match)

end

return result

end
``````
```				```
leds = { = " _  ,| | ,|_| ",

 = "  ,| ,| ",

 = " _  , _| ,|_  ",

 = "_  ,_| ,_| ",

 = "    ,|_| ,  | ",

 = " _  ,|_  , _| ",

 = " _  ,|_  ,|_| ",

 = "_   , |  , |  ",

 = " _  ,|_| ,|_| ",

 = " _  ,|_| , _| "}
``````
```				```
io.write("Enter a number: ")

for i = 1, 3 do

for j = 1, #num do

line=split(leds[tonumber(string.sub(num,j,j))]

, ",")

io.write(line[i])

end

print("")

end

print("\n")
``````

## Random Names

### We will measure the runtime

```				```
nClock = os.clock()

local names={"Anne","Gigi","Blag","Juergen","Marek",

"Ingo","Lars","Julia", "Danielle",

"Rocky","Julien","Uwe","Myles","Mike",

"Steven","Fanny"}

"Kowalkiewicz","Sauerzapf","Karg",

"Satsuta","Keene","Ongkowidjojo",

"Vayssiere","Kylau","Fenlon",

"Flynn","Taylor","Tan"}

local full_names={}
``````
```				```
for i = 1, 100000 do

table.insert(full_names,names[math.random(16)] ..

" " .. last_names[math.random(16)])

end

print("Time: " .. os.clock() - nClock)
``````

## Decimal to Romans

### This will include some nice commands...

```				```
local Roman_Table = { = "M",  = "CM",

 = "D",  = "CD",

 = "C",  = "XC",

 = "L",  = "XL",

 = "X",  = "IX",

 = "V",  = "IV",

 = "I"}
``````
```				```
local function Sort_Romans(Table)

local tab = {}

for k, v in pairs(Table) do

table.insert(tab,{key=k,value=v})

end

table.sort(tab, function(A, B)

return A.key > B.key end)

return tab

end
``````
```				```
local function Roman_Number(number, Sorted_Table)

local result = ""

while number > 0 do

for _,tab in pairs(Sorted_Table) do

if number >= tab.key then

result = result .. tab.value

number = number - tab.key

break

end

end

end

return result

end
``````
```				```
io.write("Enter a number: ")

Sorted_Table = Sort_Romans(Roman_Table)

result = Roman_Number(tonumber(num), Sorted_Table)

print(result)
``````

## Count Letters

### "This is a text file that we're going to read it using Lua"

```				```

local counter = {}

for letter in string.gmatch(line,"%w") do

counter[letter] = (counter[letter] or 0) + 1

end

for key,value in pairs(counter) do

print(key,value)

end

file:close()
``````

## Giving Lua some LOVE

### We're going to need to files...one called main.lua and the other config.lua

```				```

local myfont = love.graphics.newFont(45)

love.graphics.setFont(myfont)

love.graphics.setColor(0,0,0,225)

love.graphics.setBackgroundColor(255,153,0)

end

function love.update()

end

function love.draw()

love.graphics.print('Hello World!', 70, 150)

end
``````
```				```
function love.conf(w)

w.window.width = 400

w.window.height = 400

w.window.title = "Lua's Love"

end
``````