Lua 类型和值

Lua是动态类型语言,它有如下的类型:nil, boolean, number, string, userdata, function, thread, 和 table。
type函数用于输出类型名:

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

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

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那样修改,但是可以赋值到新的变量:

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

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

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

示例:

> 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

示例:

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

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

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

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

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

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

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

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

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

print(10 .. 20) --> 1020

建议使用tonumber:

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:

print(tostring(10) == "10") --> true
print(10 .. "" == "10") --> true

table
table类型可以用任何key来做索引,支持[]和.查找。注意用.查找的key类型必须是string类型。
table类型没有固定长度,只能用{}构造table。

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就会被自动回收内存。

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。

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

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

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

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

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

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

userdata和thread
后面会详细介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值