4.4. Lua data type

发布时间 :2023-10-12 23:00:03 UTC      

Lua is a dynamically typed language, variables do not need type definition, only need to assign values to variables. The value can be stored in a variable, passed as a parameter or returned as a result.

There are eight basic types in Lua: nil , boolean , number , string , userdata , function , thread and table .

Data type

Description

nil

This is 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

A real floating point number representing a double precision type

string

A string is represented by a pair of double or single quotation marks

function

Functions written by C or Lua

userdata

Represents a C data structure arbitrarily stored in a variable

thread

Represents an independent line for execution, used to execute collaborative programs

table

A table in Lua is actually an “associative array” (associative arrays), and the index of the array can be a number, a string, or a 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 type function tests the type of a given variable or value:

4.4.1. Example #

print(type("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 #

nil type represents a type that does not have any valid value, it has only one value – nil , for example, if you print a variable with no assignment, you will output a nil value:

> print(type(a))
nil
>

For global variables and table , nil also has a “delete” function. Assigning a nil value to a global variable or variable in a table isequivalent to deleting them. Execute the following code to determine:

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

nil should be compared in double quotation marks ": #

> type(X)
nil
> type(X)==nil
false
> type(X)=="nil"
true
>

type(X)==nil result is false , type(X) essentially, it is a string returned "nil" , which is a string type:

type(type(X))==string

Boolean (Bull) #

boolean has only two optional values for the type true (true) and false (fake) , Lua regard false and nil as false , the rest are for true , number 0 and true :

4.4.2. Example #

print(type(true))
print(type(false))
print(type(nil))

if false or nil then
    print("At least one is true")
else
    print("Both false and nil are false")
end
if 0 then
    print("The number 0 is true")
else
    print("The number 0 is false")
end

The execution result of the above code is as follows:

$ lua test.lua
boolean
boolean
nil
Both false and nil are false
The number 0 is true

number (digital) #

Lua has only one kind by default. number type– double (doubleprecision) type (default type can be modified luaconf.h , the following ways of writing are regarded as number type:

4.4.3. 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:

number
number
number
number
number
number

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 2 square brackets "[[]]" to represent a “piece” string.

Example #

html = [[
<html>
<head></head>
<body>
    <a href="http://www.runoob.com/">Novice Tutorial</a>
</body>
</html>
]]
print(html)

The following code execution results are:

<html>
<head></head>
<body>
    <a href="http://www.runoob.com/">Novice Tutorial</a>
</body>
</html>

When performing an arithmetic operation on a numeric string Lua attempts to convert this number 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 value
stack traceback:
        stdin:1: in main chunk
        [C]: in ?
>

The “error” + 1 execution in the above code reported an error, and string concatenation uses the .. , such as:

> print("a" .. 'b')
ab
> print(157 .. 428)
157428
>

Use # to calculate the length of the string, put it in front of the string, as an example:

4.4.4. Example #

> len = "www.runoob.com"
> print(#len)
14
> print(#"www.runoob.com")
14
>

table #

In Lua , table is created through a “construction expression”, and the simplest construction expression is {} to create an empty table, you can also add some data to the table to initialize the table directly:

4.4.5. Example #

-- Create an empty table
local tbl1 = {}

-- Direct Initial Table
local tbl2 = {"apple", "pear", "orange", "grape"}

The table in Lua is actually an “associative arrays” whose index can be either a number or a string.

4.4.6. 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
    print(k .. " : " .. v)
end

The execution result of the script is:

$ lua table_test.lua
key : value
10 : 33

Arrays that are different from other languages use 0 as the initial index of the array, in the Lua , the default initial index of the inner table usually starts with 1.

4.4.7. 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.lua
Key    1
Key    2
Key    3
Key    4

table will not be fixed in length, when new data is added table , the length will increase automatically, without an initial table are all nil .

4.4.8. Example #

-- table_test3.lua script file
a3 = {}
for i = 1, 10 do
    a3[i] = i
end
a3["key"] = "val"
print(a3["key"])
print(a3["none"])

The execution result of the script is:

$ lua table_test3.lua
val
nil

function #

In Lua , a function is regarded as a “First-Class Value” and can be stored in a variable:

4.4.9. Example #

-- 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 execution result of the script is:

$ lua function_test.lua
120
120

function can pass it through parameters as an anonymous function (anonymous function):

4.4.10. Example #

-- function_test2.lua script file
function testFun(tab,fun)
        for k ,v in pairs(tab) do
                print(fun(k,v));
        end
end
tab={key1="val1",key2="val2"};
testFun(tab,
function(key,val)--Anonymous function
        return key.."="..val;
end
);

The execution result of the script is:

$ lua function_test2.lua
key1 = val1
key2 = val2

thread #

In Lua , the main thread is the collaborative program ( coroutine ). It is associated with threads ( thread pretty much, with its own independent stack, local variables, and instruction pointers, you 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 thanone at the same time, while a co-program can only run one at any time, and only a running co-program is suspended suspend will not be suspended until).

Userdata (Custom Type) #

userdata is a user-defined data used to represent a type created by an application or the Cpicket + language library, and data of any data type of any Cpicket + can be transferred (usually struct and pointers) are stored to Lua is called in the variable.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.