Lua - Functions

Published 2014/03/05 by Admin in LUA

Create a function without parameters

function myFirstLuaFunction()

print("My first lua function was called")

end

Calling the function

myFirstLuaFunction()

 

Create a function with a return value

function mySecondLuaFunction()

return "string from my second function"

end

Calling the function

a=mySecondLuaFunction()

print(a)

 

Create a function with multiple parameters and multiple return values

function myFirstFunctionWithMultipleReturnValues(a,b,c)

return a,b,c,"My first lua function with multiple return values", 1, true

end

Calling the function

a,b,c,d,e,f = myFirstFunctionWithMultipleReturnValues(1,2 , "three")

print(1,b,c,d,e,f)


Lua - break statement

Published 2014/03/05 by Admin in LUA

The break statement is used to exit a loop.

a = 0

while true do

a = a+1

if a==10 then 

break

end

end

print(a)

Output:

10


Lua - Print tables

Published 2014/03/03 by Admin in LUA
Tags: , , , , ,

Printing out tables is not as simple as printing a normal variable. To print a table we must first get each element in the table by itself. We do this by using the "pairs" keyword.

a={1,2,3,4,"five","elephant","mouse"}

for i,v in pairs(a) do print(i,v) end

Output:

1     1

2     2

3     3

4     4

5     five

6     elephant

7     mouse


Lua - for statement

Published 2014/03/03 by Admin in LUA

The for statement allows you to repeat the same task for a predefined amount of iterations.

 

Count from 1 to 4 in intervals of 1

for a=1,4 do io.write(a) end

print()

Output:

1234

 

Count from 1 to 6 in intervals of 3

for a=1,6,3 do io.write(a) end

print()

Output:

14

 

Sequential iteration form

for key,value in pairs({1,2,3,4}) do print(key,value) end

Output:

1     1

2     2

3     3

4     4



Lua - while statement

Published 2014/03/03 by Admin in LUA

The while statement allows you to repeat a task until a certain condition is met.

a=1

--this is a comment

while a~= 5 do  -- Lua uses ~= to mean not equal 

a=a+1

io.write(a.." ")

end

Output:

2 3 4 5


Lua - Conditional Assignment

Published 2014/03/03 by Admin in LUA
Tags: , , , , ,

We are able to take a bit of the long-windedness of the "if, else" statement away by using conditional assignment.

a=1

b=(a==1) and "one" or "not one"

The code above is the same as the code below.

a=1

if a==1 then

b = "one"

else 

b = "not one"

end

As you can see conditional assignment removes a lot of unnecessary code.


Lua - if elseif else statement

Published 2014/03/03 by Admin in LUA

The combination of if, elseif, and else now give us more than 2 options, we can have as many cases as are required.

c=3

if c==1 then

print("c is 1")

elseif c==2 then

print("c is 2")

else

print("c isn't 1 or 2, c is "..tostring(c))

end


Lua - if else statement

Published 2014/03/03 by Admin in LUA
Tags: , , , ,

The if statement coupled with the else keyword now gives us 2 options that allows for multiple outcomes based on the conditions we are testing against.

b="happy"

if b=="sad" then

print("b is sad")

else

print("b is not sad")

end


Lua - if statement

Published 2014/03/03 by Admin in LUA
Tags: , , , ,

The if statement in coding is used for logic and sequential state machine control. Basically it helps the application decide what to do next based on a set of values that need to correspond to the logic flow requirement.

a = 1

if a==1 then

print ("a is one")

end


The following words are all reserved for internal functions in Lua.

These words cannot be used for variable names, but Lua is case sensitive, so 'and' is reserved and can't be a variable, but 'aND, aNd, AND, anD, AnD, And, ANd' are not reserved and can be used as variables.


my coding

This is a collection of all the coding gems I have found and would like to share with the world!