Lua Data Types
Lua is a dynamically typed language; variables do not need type definitions, only assignment of values. Values can be stored in variables, passed as parameters, or returned as results.
Lua has 8 basic types: nil, boolean, number, string, userdata, function, thread, and table.
| Data Type | Description |
|---|---|
| nil | This is the simplest; only the value nil belongs to this type, representing an invalid value (equivalent to false in conditional expressions). |
| boolean | It contains two values: false and true. |
| number | Represents a real floating-point number of double precision type |
| string | Strings are represented by a pair of double quotes or single quotes |
| function | Functions written in C or Lua |
| userdata | Represents any C data structure stored in a variable |
| thread | Represents an independent thread of execution, used to execute coroutines |
| table | The table in Lua is actually an "associative array", whose indices can be numbers, strings, or table types. In Lua, a table is created through a "constructor expression"; the simplest constructor expression is {}, used to create an empty table. |
We can use the type function to test the type of a given variable or value:
Example
(
type
(
"Hello world"
)
)
--> string
(
type
(
10.4
3
)
)
--> number
(
type
(
)
)
--> function
(
type
(
type
)
)
--> function
(
type
(
true
)
)
--> boolean
(
type
(
nil
)
)
--> nil
(
type
(
type
(
X
)
)
)
--> string
nil (empty)
The nil type represents a type with no valid value; it has only one value -- nil. For example, printing an unassigned variable outputs a nil value:
> print(type(a))
nil
>
For global variables and tables, nil also has a "deletion" effect; assigning a nil value to a global variable or a variable in a table is equivalent to deleting them, as you will see by executing the following code:
tab1
=
{
key1
=
"val1"
,
key2
=
"val2"
,
"val3"
}
for
k
,
v
in
pairs
(
tab1
)
do
(
k
..
" - "
..
v
)
end
tab1
.
key1
=
nil
for
k
,
v
in
pairs
(
tab1
)
do
(
k
..
" - "
..
v
)
end
Double quotes should be added when comparing nil ":
type
(
X
)
nil
type
(
X
)
==
nil
false
type
(
X
)
==
"nil"
true
type(X)==nil result is false because type(X) actually returns the "nil" string, which is of type string:
type(type(X))==string
boolean
The boolean type has only two possible values: true and false. Lua treats false and nil as false, everything else as true, and the number 0 is also true:
Example
(
type
(
true
)
)
(
type
(
false
)
)
(
type
(
nil
)
)
if
false
or
nil
then
(
"at least one is true"
)
else
(
"false and nil are both false"
)
end
if
0
then
(
"the number 0 is true"
)
else
(
"the number 0 is false"
)
end
The result of executing the above code is as follows:
$ lua test.lua
boolean
boolean
nil
false 和 nil 都为 false
数字 0 是 true
number
Lua has only one number type by default -- the double type (the default type can be modified in the definition in luaconf.h); the following forms are all regarded as number types:
Example
(
type
(
2
)
)
(
type
(
2.2
)
)
(
type
(
0.2
)
)
(
type
(
2e+1
)
)
(
type
(
0.2e-1
)
)
(
type
(
7.8263692594256e-06
)
)
Run Example »
The result of running the above code:
number
number
number
number
number
number
string
Strings are represented by a pair of double quotes or single quotes.
string1 = "this is string1"
string2 = 'this is string2'
You can also use 2 square brackets "[[]]" to represent a "block" of string.
Example
html
=
[[
Runoob Tutorial ]](
html
)
The following code executes with the result:
Runoob Tutorial
When performing arithmetic operations on a numeric string, Lua attempts to convert the numeric string into a number:
(
"2"
6
)
8.0
(
"2"
"6"
)
8.0
(
"2 + 6"
)
2
6
(
"-2e2"
"6"
)
1200.0
(
"error"
1
)
stdin
:
1
:
attempt to perform arithmetic on a
string
value
stack traceback
:
stdin
:
1
:
in
main chunk
[
C
]
:
in
?
In the above code, "error" + 1 causes an error; string concatenation uses .., as in:
> print("a" .. 'b')
ab
> print(157 .. 428)
157428
>
Use # to calculate the length of a string, placed before the string, as in the following example:
Example
len
=
"www.runoob.com"
(
len
)
14
(
"www.runoob.com"
)
14
table
In Lua, a table is created through a "constructor expression"; the simplest constructor expression is {}, used to create an empty table. You can also add some data to the table to initialize it directly:
Example
-- create an empty table
local
tbl1
=
{
}
-- initialize the table directly
local
tbl2
=
{
"apple"
,
"pear"
,
"orange"
,
"grape"
}
The table in Lua is actually an "associative array", whose indices can be numbers or strings.
Example
-- table_test.lua script file
a
=
{
}
a
[
"key"
]
=
"value"
key
=
10
a
[
key
]
=
22
a
[
key
]
=
a
[
key
]
11
for
k
,
v
in
pairs
(
a
)
do
(
k
..
" : "
..
v
)
end
The script executes with the result:
$ lua table_test.lua
key : value
10 : 33
Unlike arrays in other languages that use 0 as the initial index, the default initial index of tables in Lua generally starts at 1.
Example
-- table_test2.lua script file
local
tbl
=
{
"apple"
,
"pear"
,
"orange"
,
"grape"
}
for
key
,
val
in
pairs
(
tbl
)
do
(
"Key"
,
key
)
end
The script executes with the result:
$ lua table_test2.lua
Key 1
Key 2
Key 3
Key 4
A table does not have a fixed length or size; when new data is added, the table length automatically grows, and an uninitialized table is nil.
Example
-- table_test3.lua script file
a3
=
{
}
for
i
=
1
,
10
do
a3
[
i
]
=
i
end
a3
[
"key"
]
=
"val"
(
a3
[
"key"
]
)
(
a3
[
"none"
]
)
The script executes with the result:
$ lua table_test3.lua
val
nil
function
In Lua, functions are regarded as "first-class values", and functions can be stored in variables:
Example
-- function_test.lua script file
function
factorial1
(
n
)
if
n
==
0
then
return
1
else
return
n
factorial1
(
n
1
)
end
end
(
factorial1
(
5
)
)
factorial2
=
factorial1
(
factorial2
(
5
)
)
The script executes with the result:
$ lua function_test.lua
120
120
A function can be passed as a parameter in the form of an anonymous function:
Example
-- function_test2.lua script file
function
testFun
(
tab
,
fun
)
for
k
,
v
in
pairs
(
tab
)
do
(
fun
(
k
,
v
)
)
;
end
end
tab
=
{
key1
=
"val1"
,
key2
=
"val2"
}
;
testFun
(
tab
,
function
(
key
,
val
)
-- anonymous function
return
key
..
"="
..
val
;
end
)
;
The script executes with the result:
$ lua function_test2.lua
key1=val1
key2=val2
thread
In Lua, the most important thread is the coroutine. It is similar to a thread, having its own independent stack, local variables, and instruction pointer, and can share global variables and most other things with other coroutines.
The difference between threads and coroutines: multiple threads can run simultaneously, while only one coroutine can run at any moment, and a running coroutine is only paused when it is suspended.
userdata (custom type)
userdata is a user-defined data type used to represent a type created by an application or a C/C++ language library; it can store data of any C/C++ data type (usually structs and pointers) in a Lua variable for use.