Lua – The beginning in lua programming

Published on Author gryzli

Lua is fast, light-weight and powerfull scripting language.

If we trust the creators website, Lua must be the fastest interpreted programming language, which is a big reason to love it.

 

Lua Programming – The basis

The is no “line ending delimiters” in lua ,but if you like, you can use “;” as delimiter. The line delimiting is the same as with BASH.

Using comments

How to comment a line in lua ?

How to make a multi-line comment in lua ?

Below are the answers ..

 

Single-line comments start with “–”

-- this is a comment
a=55
print (a)

Multi-line comments

--[[
This is a 
multiline
comment
--]]
a=5
print(a)

 

Defining variables

In Lua there is no such thing as “array”, “hash”, “associative array” …etc. There is the “table” object which could be used for various types of arrays/hashes.

It is really important that you define the variable as table object, before trying to use it as such.

Below you will see how to define simple array and how to define hash in Lua.

Define a table object ( used instead of arrays/assoc arrays, hashes… etc)

-- WRONG !
table["x"] = 10 

--[[ 
Right !
Initialize the variable first
--]]

table = {} 
table["x"] = 10   # Definetable["x"] = 10 
-- OR
b="x"
table[b] = 10      # Definetable["x"] = 10

-- Define array like table 
arrayTable = {4, 5, 10, 12} 

-- Define a hash like table
hashTable =  { color="blue", thickness=55, name="Marcus", size=44 }

 

Define a variable:

string='Some_string_text'   # string is "Some_string_text"

string="Some_string_text"   # string is "Some_string_text"

string=[[Some_string_text]]  # string is "Some_string_text"

String=[==[Some_string_text]==]    # string is "Some_string_text"

 

 

Not defined variable is “nil” .

print (a)  # Prints nothing , a is "nil" 

a=5
print (a) # Prints 5

 

Define a local variable

(By default in Lua all variables are Global)

i=0;
x=5;
while i <= x do 
   local z = i
   print (z)   -- Prints z from 0 to 5
end

print (z) -- z is nill, cause it is out of scope

 

Statements

Here we also have plenty of statements, which include if/then/else,  while/do, repeat/until, for/do

Here is the little examples showing the statements you could use

a=5 

-- if/then/else
if a==5 then
   print ("a is 5") 
else
   print ("a is not 5")
end

-- 

-- While/Do statement
while a do
   print ("a is decremented to: "..a)
end

 

 

Functions

Here we will define simple function, which will “add” two numbers and return the result.

 

function math_add (a,b)
local z = a + b 
return z
end


-- Here is the calling 
result=math_add(5,10)
print (result)  -- Result is 15

 

Working with files

Working with files is simple, you are able to do standard open calls for reading and writing files.

Writing a file

fileName="temp.txt"

-- Try to open the file for writing
local fh = io.open(fileNmae,"w")

-- Check the result
if fh == nil then
print ("Error opening the file: "..fileName)
end

-- Write to file 
fh:write("Sample string to write \n")

-- Close the file
fh:close()

 

Reading from file

fileName="temp.txt"

-- Try to open the file for writing
local fh = io.open(fileNmae,"r")

-- Check the result
if fh == nil then
print ("Error opening the file: "..fileName)
end

-- Read line by line

for line in fh:lines() do 
print ("Read line: "..line.."\n")
end


-- Close the file
fh:close()

 

External resources

http://www.lua.org/pil/contents.html