Lua

Lua Programming Language Cheatsheet

Introduction

Lua is a lightweight, high-level programming language designed for embedded systems and scripting. It is known for its simplicity, flexibility, and ease of integration with other languages.

Variables and Data Types

Lua is dynamically typed and supports the following data types:

  • nil: represents the absence of a value
  • boolean: represents true or false
  • number: represents numeric values
  • string: represents textual data
  • table: represents a collection of key-value pairs
  • function: represents a block of reusable code
  • userdata: represents custom data types defined in C/C++

Control Flow

Lua provides control flow statements such as:

  • if-else: conditional statement
  • for: loop with a fixed number of iterations
  • while: loop with a condition
  • repeat-until: loop with a condition at the end
  • break: exits the current loop
  • return: exits the current function

Functions

Functions in Lua are first-class citizens and can be assigned to variables, passed as arguments, and returned as values. They can be defined using the function keyword and called using the function name followed by parentheses.

Tables

Tables in Lua are versatile data structures that can be used as arrays, dictionaries, or objects. They can store values of different types and can be indexed using keys or numerical indices.

Modules

Lua supports modular programming through the use of modules. Modules allow you to organize your code into reusable units and provide encapsulation. You can create a module by defining a table and returning it at the end of the module file.

Metatables and Metamethods

Metatables in Lua allow you to define custom behavior for tables. Metamethods are special functions that are invoked when certain operations are performed on tables, such as indexing, arithmetic operations, and comparison.

Error Handling

Lua provides a simple error handling mechanism using the pcall function, which allows you to catch and handle errors without terminating the program. You can also use the assert function to check for specific conditions and raise an error if they are not met.

Standard Library

Lua comes with a rich standard library that provides various modules for common tasks such as string manipulation, file I/O, networking, and more. You can use the require function to load and use these modules in your code.

hello = "hi world"        -- global variable
local hello = "hi local world" -- local variable
print(hello)

function doMath(n)
    return n * 2
end
print(doMath(2))
callMeLater(domMath) --can use in ohter functions for functional programing

array = { "landing", "on", "lua" }
dict = {
    ["moon"] = "yes",
    ["cheese"] = "no"
}
for k, v in pairs(dict) do
    print(k, v)
end

co = coroutine.create(function()
    coroutine.yield('beginning')
    coroutine.yield('middle')
    return 'end'
end)
coroutine.resume(co) --beggining
coroutine.resume(co) --middle
coroutine.resume(co) --end
coroutine.resume(co) --error

Video Video3 Video Link1 Link1 Link1 Link1