In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "what are the basic types of Lua". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "what are the basic types of Lua" can help you solve the problem.
Lua is a dynamically typed language. There is no type-defined syntax in the language, and each value has its own type information. There are 8 basic types in Lua.
The data type describes nil as the simplest, and only the value nil belongs to this class, representing an invalid value (equivalent to false in a conditional expression). Boolean contains two values: false and true. Number represents the double precision type of real floating point string string by a pair of double quotation marks or single quotation marks to represent the function function written by C or Lua userdata represents any C data structure thread stored in the variable represents the independent line of execution, the table (table) used to execute the collaborative program tableLua is actually an "associative array" (associative arrays), the index of the array can be a number, string or table type. In Lua, table is created through a "construction expression". The simplest construction expression is {}, which is used to create an empty table.
We can use the type function to test the type of a given variable or value:
Example
Print (type ("Hello world"))-- > stringprint (type (10.4x3))-- > numberprint (type (print))-- > functionprint (type (type))-- > functionprint (type (true))-- > booleanprint (type (nil))-- > nilprint (type (type (X)-- > string
Nil (empty)
The nil type represents a type that does not have any valid value, but has only one value-nil. For example, printing an unassigned variable will output a nil value:
Print (type (a)) nil >
There is also a "delete" function for global variables and table,nil. Assigning a nil value to global variables or variables in the table table is equivalent to deleting them. Just execute the following code:
Tab1 = {key1 = "val1", key2 = "val2", "val3"} for k, v in pairs (tab1) do print (k.. "-". V) endtab1.key1 = nilfor k, v in pairs (tab1) do print (k.. "-". V) end
Nil should be compared in double quotation marks:
> type (X) nil > type (X) = = nilfalse > type (X) = = "nil" true >
Type (X) = = the reason why the nil result is false is that type (X) is essentially a returned "nil" string, which is a string type:
Type (type (X)) = = string
Boolean (Bull)
There are only two optional values for the boolean type: true (true) and false (false). Lua treats false and nil as false, the others are true, and the number 0 is also true: instance
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 both false") endif 0 then print ("number 0 is true") else print ("number 0 is false") end
The execution result of the above code is as follows:
$lua test.luabooleanbooleannilfalse and nil are both false numbers 0 is true
Number (digital)
By default, there is only one number type for Lua-double (double precision) type (the default type can be modified by definition in luaconf.h). The following words are all regarded as number types:
Example
Print (type (2)) print (type (2.2) print (type (0.2)) print (type (2e+1)) print (type (0.2e-1)) print (type (7.8263692594256e-06))
The result of the above code execution:
Numbernumbernumbernumbernumbernumber
String (string)
The string is represented by a pair of double or single quotation marks.
String1 = "this is string1" string2 = 'this is string2'
You can also use two square brackets "[[]]" to represent a "one" string.
Example
Html = [["http://www.runoob.com/"> Rookie course]] print (html)
The following code execution results are:
"http://www.runoob.com/"> rookie tutorial
When performing an arithmetic operation on a numeric string, Lua attempts 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 perform arithmetic on a string valuestack traceback: stdin:1: in main chunk [C]: in? >
The "error" + 1 execution in the above code reported an error, and the string concatenation uses.. , such as:
Print ("a".. 'b') ab > print. 157428 >
Use # to calculate the length of the string, put it in front of the string, as an example:
Example
> len = "www.runoob.com" > print (# len) 14 > print (# "www.runoob.com") 14 > Guide table (table)
In Lua, table is created through a "construction expression". The simplest construction expression is {}, which is used to create an empty table. You can also add some data to the table to initialize the table directly:
Example
-- create an empty tablelocal tbl1 = {}-- directly initialize table local tbl2 = {"apple", "pear", "orange", "grape"}
The table in Lua is actually an "associative array" (associative arrays), and the index of the array can be a number or a string.
Example
-- table_test.lua script file a = {} a ["key"] = "value" key = 10a [key] = 22a [key] = a [key] + 11for k, v in pairs (a) script (k. ":" V) end
The execution result of the script is:
$lua table_test.luakey: value10: 33
Unlike arrays in other languages, which use 0 as the initial index of the array, the default initial index of a table in Lua usually starts with 1.
Example
-- table_test2.lua script file local tbl = {"apple", "pear", "orange", "grape"} for key, val in pairs (tbl) do print ("Key", key) end
The execution result of the script is:
$lua table_test2.luaKey 1Key 2Key 3Key 4
Table does not have a fixed length, and the length of table increases automatically when new data is added. Without the initial table, it is always nil.
Example
-- table_test3.lua script file A3 = {} for I = 1,10 do A3 [I] = ienda3 ["key"] = "val" print (A3 ["key"]) print (A3 ["none"])
The execution result of the script is:
$lua table_test3.luavalnil
Function (function)
In Lua, a function is regarded as a "First-Class Value", and a function can be stored in a variable:
Example
-- function_test.lua script file function factorial1 (n) if n = 0 then return 1 else return n * factorial1 (n-1) endend > print (factorial1 (5)) factorial2 = factorial1print (factorial2 (5))
The execution result of the script is:
$lua function_test.lua120120
Function can be passed through parameters as an anonymous function (anonymous function):
Example
-- function_test2.lua script file function testFun (tab,fun) for kjorie v in pairs (tab) do print (fun (krecov)); endendtab= {key1= "val1", key2= "val2" style= "color: Olive;" >}; testFun (tab,function (key,val)-- anonymous function return key.. "="... Val; end)
The execution result of the script is:
$lua function_test2.luakey1 = val1key2 = val2
Thread (Thread)
In Lua, the main thread is the collaborative program (coroutine). It is similar to thread, with its own independent stack, local variables, and instruction pointers, and can share global variables and most other things with other co-programs.
The difference between a thread and a co-program: a thread can run more than one at the same time, while a co-program can only run one at any time, and a running co-program is paused only when it is suspend.
Userdata (Custom Type)
Userdata is a user-defined data used to represent a type created by an application or the Cmax Cure + language library, and data of any data type (usually struct and pointer) of any data type of Cpicket + can be stored in the Lua variable.
This is the end of the content about "what are the basic types of Lua". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.