lua - aRustyDev/CodeWars GitHub Wiki

Lua

  • Variables
  • String manipulation
  • Operators
  • Syntax
  • Data Structures
  • Data Types

Variables

In Lua, though we don't have variable data types, we have three types based on the scope of the variable.

  1. Global Variables − All variables are considered global unless explicitly declared as a local.
  2. Local Variables − When the type is specified as local for a variable then its scope is limited with the functions inside their scope.
  3. Table Fields − This is a special type of variable that can hold anything except nil including functions.

Examples

Basic Structure of variable declaration: <type> <variable_list>;

Local Variables

local    i, j
local    i
local    a,c

Global Variables

d , f = 5, 10; 

String manipulation

Operators

+, -, *, /, %, ^

==, ~=, >, <, >=, <=

'and', 'or', 'not'

.. #

Left -> Right : *, /, %, +, -, <, >, <=, >=, ==, ~=, ==, ~=, 'and', 'or'
Right -> Left : 'not', #, -, ..

Syntax

Data Structures

  • Tables
  • Iterators
  • Arrays
  • Strings

Data Types

# Type Description
1 nil Used to differentiate the value from
having some data or no(nil) data
2 boolean Includes true and false as values.
Generally used for condition checking.
3 number Represents real numbers.
ie: Double Precision Floating Point
4 string Represents array of characters
5 function Represents a method that is written
in C or Lua.
6 userdata Represents arbitrary C data.
7 thread Represents independent threads of
execution and it is used to implement
coroutines.
8 table Represent ordinary arrays, symbol tables,
sets, records, graphs, trees, etc.,
and implements associative arrays.
It can hold any value (except nil).

Check Type

print(type("What is my type"))   --> string
t = 10

print(type(5.8*t))               --> number
print(type(true))                --> boolean
print(type(print))               --> function
print(type(nil))                 --> nil
print(type(type(ABC)))           --> string

Sources

  1. Tutorials Point
⚠️ **GitHub.com Fallback** ⚠️