lua 数据类型

Lua中有8个基本类型分别为:nil、boolean、number、string、userdata、function、thread和table。

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 类型表示没有任何有效值,它只有一个值 -- nil,例如打印一个没有赋值的变量,便会输出一个 nil 值

> print(type(a))
nil
>

nil 作比较时应该加上双引号 "

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

type(X)==nil 结果为 false 的原因是因为 type(type(X))==string

如果没有使用type(变量)这个形式,就不需要加引号

print(x == nil) --结果为true
print(x == "nil")--结果为false

boolean(布尔)

print(type(true))
print(type(false))
print(type(nil))
 
if false or nil then
    print("至少有一个是 true")
else
    print("false 和 nil 都为 false!")
end

输出:

$ lua test.lua 
boolean
boolean
nil
false 和 nil 都为 false!

number(数字)

Lua 默认只有一种 number 类型 -- double(双精度)类型(默认类型可以修改 luaconf.h 里的定义),以下几种写法都被看作是 number 类型

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))

输出:

number
number
number
number
number
number

 

string(字符串)

字符串由一对双引号或单引号来表示

string1 = "this is string1"
string2 = 'this is string2'

可以用 2 个方括号 "[[]]" 来表示"一块"字符串

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

输出:

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

对一个数字字符串上进行算术操作时,Lua 会尝试将这个数字字符串转成一个数字

> print("2" + 6)
8.0
> print("2" + "6")
8.0
> print("2 + 6")
2 + 6
> print("-2e2" * "6")
-1200.0

字符串连接使用的是 .. (注:当在一个数字后面写 .. 时,必须加上空格以防止被解释错)

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

用 # 来计算字符串的长度,放在字符串前面

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

使用 # 输出的值其实是字符串所占的字节数。当字符串为英文字符时,由于每个字符只占 1 个字节,所以输出结果等同于字符串长度

print(#"HelloWorld")

 输出:10

当字符串中含有中文字符时,每个中文字符占据两个字节:

print(#"你好世界")

 输出:8



table(表)

{}用来创建一个空表。也可以在表里添加一些数据,直接初始化表

-- 创建一个空的 table
local tbl1 = {}
 
-- 直接初始表
local tbl2 = {"apple", "pear", "orange", "grape"}

实例

-- table_test.lua 脚本文件
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

输出

$ lua table_test.lua 
key : value
10 : 33

在 Lua 里表的默认初始索引一般以 1 开始

-- table_test2.lua 脚本文件
local tbl = {"apple", "pear", "orange", "grape"}
for key, val in pairs(tbl) do
    print("Key", key)
end

输出:

$ lua table_test2.lua 
Key    1
Key    2
Key    3
Key    4

table 不会固定长度大小,有新数据添加时 table 长度会自动增长,没初始的 table 都是 nil

-- table_test3.lua 脚本文件
a3 = {}
for i = 1, 10 do
a3[i] = i
end
a3["key"] = "val"
print(a3[5])
print(a3["key"])
print(a3["none"])

 输出:

$ lua table_test3.lua

5
val
nil

实例

tab = {"Hello","World",a=1,b=2,z=3,x=10,y=20,"Good","Bye"}
for k,v in pairs(tab) do
    print(k.."  "..v)
end

输出

1  Hello
2  World
3  Good
4  Bye
x  10
y  20
z  3
b  2
a  1

如上代码输出结果存在一定规律,"Hello"、"World"、"Good"、"Bye"是表中的值,在存储时是按照顺序存储的,不同于其他脚本语言,Lua是从1开始排序的

使用pairs遍历打印输出时,会先按照顺序输出表的值,然后再按照键值对的键的哈希值顺序打印

function(函数)

 在 Lua 中,函数是被看作是"第一类值(First-Class Value)",函数可以存在变量里

-- function_test.lua 脚本文件
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))

输出:

$ lua function_test.lua 
120
120

function 可以以匿名函数(anonymous function)的方式通过参数传递

-- function_test2.lua 脚本文件
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)--匿名函数
    return key.."="..val;
end
);

输出:

$ lua function_test2.lua 
key1 = val1
key2 = val2

实例

-- function_test2.lua 脚本文件
function testFun(tab,fan)
    for k ,v in pairs(tab) do
        print(fan(k,v));
    end
end


tab={key1="val1",key2="val2"};
testFun(tab,
function(key,val)--匿名函数1
    return key.."="..val;
end
)

tab2={key1="val3",key2="val4"};
testFun(tab2,
function(key,val)--匿名函数2
    return key.."+"..val;
end
)

输出

key1=val1
key2=val2
key1+val3
key2+val4

thread(线程)

在 Lua 里,最主要的线程是协同程序(coroutine)。它跟线程(thread)差不多,拥有自己独立的栈、局部变量和指令指针,可以跟其他协同程序共享全局变量和其他大部分东西。

线程跟协程的区别:线程可以同时多个运行,而协程任意时刻只能运行一个,并且处于运行状态的协程只有被挂起(suspend)时才会暂停

userdata(自定义类型)

userdata 是一种用户自定义数据,用于表示一种由应用程序或 C/C++ 语言库所创建的类型,可以将任意 C/C++ 的任意数据类型的数据(通常是 struct 和 指针)存储到 Lua 变量中调用

 
原文地址:https://www.cnblogs.com/sea-stream/p/9978135.html