the cedar ledge

A Beginner's to Master's Guide in Learning Lua

Date: October 1 2022

Summary: My notes on learning the Lua programming language

Keywords: #archive #lua #quick #start #programming

Bibliography

https://github.com/medwatt/Notes/blob/main/Lua/LuaQuickGuide.ipynb

Table of Contents

    1. Reading Motivation
    2. Comments
    3. Variable Scope
      1. Global Scope
      2. Local Scope
    4. Variable Assignments
    5. Variable Types
    6. Math Operators
    7. Relational Operators
    8. Logical Operators
  1. How To Cite
  2. References:
  3. Discussion:

Reading Motivation

I wanted to learn Lua for no other reason than to program my own workflow in neovim. Also, Lua is cool and Just-in-Time compiled like Julia! Plus, it is used for game development so if ever this research thing doesn't work out, backup plan: become a game developer.

Comments

How to write comments within Lua!

-- This is my comment! 

--[[ 
	This
	is 
	a 
	multiline 
	comment! 
--]]

Variable Scope

Global Scope

All variables within lua are considered global by default. Moreover, each call or line within a lua script is run as a chunk.

Var = 10
print( "Global var:", Var )

Local Scope

To define a large chunk where variables are local in scope, one can create a local scope using a do-end block

do
	local Var = 20
	print( "Local Var:", Var )
end

Variable Assignments

A = A + 1 -- This works 
A += 1 -- This does not work 

B = 0 -- This works
C = 0 -- This works
B, C = 0, 0 -- This works
B = C = 0 -- This does not work

Variable Types

There are eight basic types within Lua:

print( type("Hello") ) -- string 
print( type(10.4 * 3) ) -- number 
print( type({1, 2, 3}) ) -- table
print( type(print) ) -- function 
print( type(true) ) -- boolean 
print( type(nil) ) -- nil

NOTE: Arithmetic operations applied to an object of type string will not fail but instead Lua will attempt to convert that object to a number type. Vice-versa to this is that if a object of type number is used in a string operation, Lua will attempt to convert that object to a string.

Math Operators

A = 100
B = 2 

print( A + B ) -- Addition
print( A - B ) -- Subtraction 
print( A * B ) -- Multiplication
print( A ^ B ) -- Exponents 
print( A / B ) -- Division
print( A // B ) -- Floor
print( A % B ) -- Modulus
print( -A ) -- Negation

Relational Operators

A = 20
B = 4

print(A == B) -- false
print(A ~= B) -- true
print(A > B) -- true
print(A < B) -- false
print(A >= B) -- true
print(A <= B) -- false

Logical Operators

print(true and 10) -- evaluates to `10`
print(10 and true) -- evaluates `true`
print(false and 10) -- short circuits to `false`
print(false or 10) -- evaluates to `10`
print(nil and 10) -- evaluates to `nil`
print(nil or 10) -- evaluates to `10`
print(false and nil) -- evaluates to `false`
print(false and not(nil)) -- evaluates to `false`

How To Cite

Zelko, Jacob. A Beginner's to Master's Guide in Learning Lua. https://jacobzelko.com/10012022184345-learning-lua. October 1 2022.

References:

Discussion:

CC BY-SA 4.0 Jacob Zelko. Last modified: May 19, 2024. Website built with Franklin.jl and the Julia programming language.