您的位置:首页 > 编程语言 > Lua

Lua 类型和值

2016-01-13 19:29 375 查看
Lua是动态类型语言,它有如下的类型:nil, boolean, number, string, userdata, function, thread, 和 table。

type函数用于输出类型名:

[code]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


变量没有预定义类型,任何变量都包括其它类型的值:

[code]print(type(a)) --> nil ('a' is not initialized)
a = 10
print(type(a)) --> number
a = "a string!!"
print(type(a)) --> string
a = print -- yes, this is valid!
a(type(a)) --> function


Nil

Nil的性质是区别于其他值的特殊类型。没有赋值的变量其值为Nil,你也可以用过赋值Nil来删除它。

boolean

布尔类型有两个值:true和false。所有false和ni都会被视为false,其它则为true(包括0和空字符串)。

number

Lua没有整型,只有双精度浮点型数。只要整数不超过10^16次方,就不会发生回绕。

string

Lua中的string值不可以像C那样修改,但是可以赋值到新的变量:

[code]a = "one string"
b = string.gsub(a, "one", "another") -- change string parts
print(a) --> one string
print(b) --> another string


在字符串前面加#可以获得字符串长度:

[code]a = "hello"
print(#a) --> 5
print(#"good\0bye") --> 8


你可以用”“也可以用”来表示字符串。”“里面’不会终止字符串,”里面”不会终止字符串。

字符串转换符常量,跟C差不多:

\a bell

\b back space

\f form feed

\n newline

\r carriage return

\t horizontal tab

\v vertical tab

\ backslash

\” double quote

\’ single quote

示例:

[code]> print("one line\nnext line\n\"in quotes\", 'in quotes'")
one line
next line
"in quotes", 'in quotes'
> print('a backslash inside quotes: \'\\\'')
a backslash inside quotes: '\'
> print("a simpler way: '\\'")
a simpler way: '\'


十进制和十六进制表示字符

十进制:\ddd(必需占3位,不足3位前面用0补)

十六进制:\x\hh

示例:

[code]"alo\n123\""
'\97lo\10\04923"
'\x61\x6c\x6f\x0a\x31\x32\x33\x22'


长字符串,可以用[[和]]来括住多行文本:

[code]page = [[
<html>
<head>
    <title>An HTML Page</title>
</head>
<body>
    <a href="http://www.lua.org">Lua</a>
</body>
</html>
]]
write(page)


你可以在[和[之间加任意个=号来指示独特的开头和结尾符。

\z会忽略空字符串,直到遇到非空字符串。因此可以用于换行。

[code]data = "\x00\x01\x02\x03\x04\x05\x06\x07\z
        \x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"


自动转换:

+和*等是数字计算操作符,只能操作数字,因此会自动将字符串转化为数字。

[code]print("10" + 1) --> 11
print("10 + 1") --> 10 + 1
print("-5.3e-10"*"2") --> -1.06e-09
print("hello" + 1) -- ERROR (cannot convert "hello")


..是字符连接操作符,将..左右字符串连接起来。如果是数字,则将数字转换为字符串,但是当前面有数字的时候,必需加空格,否则会识别为小数点。

[code]print(10 .. 20) --> 1020


建议使用tonumber:

[code]line = io.read() -- read a line
n = tonumber(line) -- try to convert it to a number
if n == nil then
    error(line .. " is not a valid number")
else
    print(n*2)
end


或者使用tostring:

[code]print(tostring(10) == "10") --> true
print(10 .. "" == "10") --> true


table

table类型可以用任何key来做索引,支持[]和.查找。注意用.查找的key类型必须是string类型。

table类型没有固定长度,只能用{}构造table。

[code]a = {} -- create a table and store its reference in 'a'
k = "x"
a[k] = 10 -- new entry, with key="x" and value=10
a[20] = "great" -- new entry, with key=20 and value="great"
print(a["x"]) --> 10
k = 20
print(a[k]) --> "great"
a["x"] = a["x"] + 1 -- increments entry "x"
print(a["x"]) --> 11


table是一个无名对象,当没有任何变量引用它时,该table就会被自动回收内存。

[code]a = {}
a["x"] = 10
b = a -- 'b' refers to the same table as 'a'
print(b["x"]) --> 10
b["x"] = 20
print(a["x"]) --> 20
a = nil -- only 'b' still refers to the table
b = nil -- no references left to the table


table可以使用不同类型的key。

[code]a = {} -- empty table
-- create 1000 new entries
for i = 1, 1000 do a[i] = i*2 end
print(a[9]) --> 18
a["x"] = 10
print(a["x"]) --> 10
print(a["y"]) --> nil


也可以像数组那样使用它,只是无需分配大小。

[code]-- read 10 lines, storing them in a table
a = {}
for i = 1, 10 do
a[i] = io.read()
end


在表前面加#号可以得出前面有多少个非nil值,注意如果隔着nil的值是不被计入的。

[code]-- print the lines
for i = 1, #a do
print(a[i])
end


function

Lua中的函数可以作为变量储存起来,可以作为参数传递,或者作为函数返回结果。

userdata和thread

后面会详细介绍
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: