Lua data type
Lua is a dynamic type language, variables do not type definitions, only need to assign values for variables. Values can be stored in variables, passed as arguments or returned.
Lua has eight basic types: nil, boolean, number, string, userdata, function, thread and table.
type of data | description |
---|---|
Nil | This is the simplest, only the value nil belongs to the class, representing an invalid value (equivalent to false in the conditional expression). |
Boolean | Contains two values: false and true. |
Number | Represents a real floating point number for a double type |
String | A string is represented by a pair of double quotation marks or single quotation marks |
Function | A function written by C or Lua |
Userdata | Represents a C data structure that is stored in a variable |
Thread | An independent line that is executed for execution of a collaborative procedure |
Table | Lua table (table) is actually a "associative array" (associative arrays), the index of the array can be a number or a string. In Lua, the creation of table is done by "constructing an expression". The simplest constructor is {}, which is used to create an empty table. |
We can use the type function to test the given variable or the type of value:
Print ( type ( "the Hello World" )) -> String Print ( type ( 10.4 * . 3 )) -> Number Print ( type ( Print )) -> function Print ( type ( type )) -> function Print ( Type ( true )) -> boolean print ( type ( nil )) ->Nil print ( type ( type ( X ))) -> string
Nil
The nil type indicates that there is no valid value, it has only one value - nil, for example to print a variable that has no assignment, it will output a nil value:
> Print ( type ( a )) nil >
For the global variables and table, nil there is a "delete" role, to the global variable or table table variables assigned a nil value, equivalent to delete them, the implementation of the following code to know:
Tab1 = { key1 = "val1" , key2 = "val2" , "val3" } for k , v in pairs ( tab1 ) do print ( k .. "-" .. v ) end Tab1 . Key1 = nil for k , v in pairs ( tab1 ) do print ( k .. "-" .. v ) end
Boolean (boolean)
Boolean type has only two optional values: true (true) and false (false), Lua false and nil as "false", the other are "true":
Print ( type ( true )) print ( type ( false )) print ( type ( nil )) If false or nil then print ( "at least one is true" ) else print ( "false and nil are false!" ) End
The above code execution results are as follows:
$ Lua test . Lua boolean boolean nil false and nil are false !
Number (number)
Lua default only one type type - double (double type) (the default type can modify the definition of luaconf.h), the following wording are considered as number type:
Print ( type ( 2 )) Print ( type ( 2.2 )) Print ( type ( 0.2 )) Print ( type ( 2E +. 1 )) Print ( type ( 0.2 e. 1- )) Print ( type ( 7.8263692594256e-06 ))
Running examples »
The above code execution results:
Number
Number
Number
Number
Number
Number
String (string)
A string is represented by a pair of double quotation marks or single quotation marks.
String1 = "this is string1" string2 = 'this is string2'
You can also use 2 square brackets "[[]" to represent "a" string.
HTML = [[ <HTML> <head> </ head > <body> < A the href = "http://www.w3cschool.cc/" > W3Schools novice tutorial </ A > </ body > </ HTML > ] ] Print ( html )
The following code execution results are:
<HTML> <head> </ head> <body> <a "http://www.w3cschool.cc/" the href => W3Schools novice tutorial </a> </ body> </ HTML>
When an arithmetic operation is performed on a numeric string, Lua tries to convert the numeric string to a number:
> Print ( "2" + . 6 ) 8.0 > Print ( "2" + ". 6" ) 8.0 > Print ( "2 +. 6" ) 2 + . 6 > Print ( "-2e2" * ". 6" ) - 1200.0 > Print ( "Error" + 1 ) stdin : 1 : attempt to have arithmetic on a string value Stack traceback : stdin : 1 : in main chunk [ C ]: in ? >
The above code "error" + 1 implementation of the error, the string connection is used, such as:
> Print ( "a" .. 'b' ) Ab > Print ( 157 .. 428 ) 157428 >
Use # to calculate the length of the string, preceded by the string, as follows:
> Len = "www.w3cschool.cc" > print (# len ) 16 > print (# "www.w3cschool.cc" ) 16 >
Table (table)
In Lua, the creation of table is done by "constructing an expression". The simplest constructor is {}, which is used to create an empty table. You can also add some data in the table, the direct initialization table:
- create an empty table local tbl1 = {} - direct initial table local tbl2 = { "apple" , "pear" , "orange" , "grape" }
Lua table (table) is actually a "associative array" (associative arrays), the index of the array can be a number or a string.
- a ( key ) = a [ key ] + 11 for k , v in pairs ( a ) - the table_test . Lua script file a = {} a [ "key" ] = "value" key = 10 a [ key ] = 22 a [ Do print ( k .. ":" .. v ) end
The result of the script execution is:
$ Lua table_test . Lua Key : value 10 : 33
An array other than other languages takes 0 as the initial index of the array, and the default initial index of the table in Lua generally starts at 1.
- table_test2 . Lua script files local TBL = { "Apple" , "PEAR" , "Orange" , "Grape" } for Key , Val in pairs ( TBL ) do Print ( "Key" , Key ) End
The result of the script execution is:
$ Lua table_test2 . Lua Key 1 Key 2 Key 3 Key 4
Table will not be fixed length, there are new data to add the table length will automatically grow, no initial table are nil.
- table_test3 . Lua script files A3 = {} for I = . 1 , 10 do A3 [ I ] = I End A3 [ "Key" ] = "Val" Print ( A3 [ "Key" ]) Print ( A3 [ "none " ])
The result of the script execution is:
$ Lua table_test3 . Lua Val Nil
Function (function)
In Lua, the function is treated as a "first-class value", the function can exist in the variable:
- function_test . Lua script file function factorial1 ( n ) if n == 0 then return 1 else return n * factorial1 ( n - 1 ) end end print ( factorial1 ( 5 )) factorial2 = factorial1 print ( factorial2 ( 5 ))
The result of the script execution is:
$ Lua function_test . Lua 120 120
Function can be passed as an anonymous function via parameters:
- function_test2 . Lua script file function anonymous ( tab , fun ) for k , v in pairs ( tab ) do print ( fun ( k , v )) end end tab = { key1 = "val1" , key2 = "val2" } Anonymous ( tab , function ( key , val ) return key ."=" .. val end )
The result of the script execution is:
$ Lua function_test2 . Lua Key1 = val1 Key2 = val2
Thread (thread)
In Lua, the most important thread is the collaborative process (coroutine). It is almost the same as the thread (thread), have their own independent stack, local variables and instruction pointers, you can share with other collaborative programs global variables and most other things.
The difference between threads and joysticks: threads can run at the same time, and the protocol can only run one at any time, and the running state of the transaction is suspended only suspend (suspend).
Userdata (custom type)
Userdata is a user-defined data that is used to represent a type created by an application or a C / C ++ language library. Any data of any C / C ++ data (usually struct and pointers) can be stored in Lua Variable in the call.