lua mysql 存储类型_Lua学习----Lua基础数据类型

前言

Lua有6中数据类型,分别是nil(空)、boolean(布尔)、number(数字)、string(字符)、table(表)、function(函数)

在Lua中可以使用type函数来返回一个值或者变量所属的类型,如:

print(type("helle world"))-->output:string

print(type(print))-->output:function

print(type(true))-->output:boolean

1.nil(空)

Lua将nil用来表示“无效值”。一个变量再第一次赋值前默认值是nil,将nil赋值给一个全局变量就等同于删除它。

local num

print(num)-->output:nil

num = 100

print(num)-->output:100

num = nil            print(num)             -->output:nil

2.boolean(布尔)

布尔值可选值为true/false,在Lua中nil和false为“假”,其余都为“真”,比如0和空字符都为真。

3.number(数字)

number用于表示实数,可以使用数学函数math.floor(向下取整),math.ceil(向上取整)

localorder=3.0

localscore=98.5

print(math.floor(order))-->output:3

print(math.ceil(score))-->output:99

4.string(字符)

在Lua中,字符串有三种表示方法:

1)使用一对单引号。如:‘hello’

2)使用一对双引号。如:“hello”

3)使用长括号(即[[]])来定义。

注:Lua的字符串中的转移字符不起作用。

Lua的字符串是不可改变的值,不能像再c语言中那样直接修改字符串的某个字符,而是根据修改要求来创建一个新的字符串。

Lua也不能通过下标来访问字符串的某个字符。

localstr3 = [["add\name",'hello']]

localstr4 = [=[stringhavea[[]].]=]

print(str1)-->output:helloworld

print(str2)-->output:hellolua

print(str3)-->output:"add\name",'hello'

print(str4)-->output:string have a [[]].

5.table(表)

table类型实现了一种抽象的“关联数组”。关联数组是一种具有特殊索引方式的数组,索引通常是字符串或者number类型,但也可以是除nil以外任意类型的值。

6.function(函数)

在Lua中,函数也是一种数据类型,函数可以存储在变量中,可以通过参数传递给其他函数,还可以作为其他函数的返回值。

函数以end结尾。

local function foo()

print("in the function")

--dosomething()

local x = 10

local y = 20

return x + y

end

local a = foo--把函数赋给变量

print(a())

--output:

in the function

30

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值