Friday, August 28, 2020

Lua quick start guide

Quick start guide to lua

Introduction

This is my quick and basic introduction to lua.
In this guide i will quickly walk you through the basics of lua.

What this guide is not

This guide is not a step by step introduction to programming.
Nor will this guide cover a bunch of computer science concepts.

What this guide is

This guide assume you have some understating of programming and computer science.
This guide is simply a quick introduction to the basics of lua.
This guide assumes you are familiar with basic programming and want to cover the basics of lua.

Install basics

This guide wont go into the details of installation and running a lua program.
That information is pretty well conveyed by the lua website.
once you have lua set up you can continue the rest of this guide.

The Basics of variables

The very first thing to learn is how lua handles variables.
With lua there is no type declarations or even a var keyword.
To use a variable, simple use the variable name and assign it a value, as seen in example below
You can find all the example code used in this guide over on github.

Variables in lua
Example of using variables in lua

Variables can be assigned to any type of data, int, floats, strings, etc.
Variables can also be reassigned to a different type.
For example, a variable holding a string can then be assigned to hold an int.
By default all variables are global. To make a variable local, use the local keyword.

Local variables in lua
local variables.

Functions

Functions are declared by using the function keyword.
Return types are not declared as they are in languages such as C.
Functions are closed with the end keyword.

functions in lua
functions in lua.

Functions declared local have their scope limited the same as variables.
Local functions can only be called below them in the file.
Local functions can be quicker than non local function.

Functions can return no data, a single variable, or many variables.
Functions return multiple items as separate variables.
If you try to assign more variables to a function than it returns, the excess variables are assigned the value nil

tables

Tables are lua's storage device.
Understanding tables is a very big key to being good at lua.
Tables can be used as dynamic arrays are used in other languages.
Tables can also be used like associative arrays or dictionaries.
You can store almost anything you want in a table,and aren't limited to a single data type per table.
One thing to keep in mind: Tables start at 1 not 0.

tables in lua
Tables in lua.

Inserting data into tables

inserting elements into tables
Inserting elements into a table.

There are two main ways to insert data into an array: using an index and using table.insert()
There are two forms of table.insert():

  • table.insert(mytable,element)
    • This will place the element at the end of the given table.
  • table.insert(mytable,index,element)
    • this one inserts the element at the given index into the table.

You can also assign the return data from a function to be elements in a table.
When assigning from a function returning multiple items, each item will be a sequential element in the table.

Retrieving data from table

Accessing elements in tables
Retrieving data from a table.

Retrieving data from a table used as an array is simple: just use the index like you would in most other language.
For tables used as associative array/dictionaries, you have two options:

  • use the key in mytable[key] like using an index.
  • Use mytable.key.

Removing elements from a table

Removing items from a table is easy, just use table.remove(mytable,index).
For an associative array/dictionary table, set the key to nil to remove the key,value pair.

Removing elements from a table
Removing elements from a table.

sorting tables

Sorting tables is pretty easy. lua includes a built in quicksort for this.

sorting tables in lua
sorting tables.
  • table.sort(mytable)
    • This sorts the elements in the given table numerically.
  • table.sort(mytable,compr)
    • this version takes in a function to do the comparison.
    • Using a custom compare function lets you sort elements in a table however you like.

That covers the basics of using tables. But to get really good, you should read up on metatables.
Metatables cover the inner workings and properties of a table.
You can get tables to do some interesting things by changing its metatable.

Printing

Printing to stdout is fairly simple
The standard way to do so is to use io.write()
Lua does have a print() function, but io.write() is the standard one to use.

printing to STDOUT in lua
Print to stdout.

Control flow

If you are familiar with any other modern programming language, then this section should be familiar.
Only things to note are lua syntax specific things such as:

  • if statements need an end just like functions.
  • Lua uses and for logic AND.
  • Lua uses or for logic OR.
  • Lua uses ~= for not equal to.
  • Lua uses not for logical NOT.
  • elseif is one word.
Control flow in lua
control flow in lua.

Loops

Loops in lua should be easy to grasp for anyone with programming experience.

  • Syntax for a standard for loop is var=start,stop,increment do.
  • repeat until("condition") loops until condition in until() is true
    • So you can think of it like a do while() loop but the condtion is reversed
  • ipairs() iterates over a table giving you an index and element on each iteration.
  • pairs() iterates over a table giving you a key and value on each iteration.
  • loops need an end just like functions and if statements.
Loops in lua
Loops in lua.

File I/O

Basic file I/O is fairly simple.
The example code pretty much sums up the basics.
There are a few functions such as fseek() and flush() which are not covered here.
I felt that those functions were beyond the scope of my very basic introduction guide.

Reading files in lua
Reading files in lua.
Writing files in lua
writing to files in lua.

Math functions

The math library for lua has the typical math functions you'd expect.
Again, if you have any programming experience then you probably already know these.
These are the basics, but lua has a few more.

Loops in lua
Math functions in lua.

String Functions

Some of the more commonly used and useful string functions in lua.
The only thing to take note of is that the regex flavor used by lua may differ a bit from what you find elsewhere.
Needles to say, but a look at the lua reference on regex would be beneficial.

String functions in lua
string functions in lua.

Final remarks

Thank you for checking out my basic introduction to programming in lua.
I intentionally left out things which I felt were beyond the basic scope and goal of this guide.
Two big things which are missing are metatables and OOP.
I felt that there is just too much to cover for those topics and they each deserve their own separate posts.
You can find all the example code used in this guide over on github.

No comments:

Post a Comment