標籤:pre 括弧 class 引號 ber 方式 type 基礎資料類型 end
前言
Lua有6中資料類型,分別是nil(空)、boolean(布爾)、number(數字)、string(字元)、table(表)、function(函數)
在Lua中可以使用type函數來返回一個值或者變數所屬的類型,如:
print(type("helle world"))-->output:stringprint(type(print))-->output:functionprint(type(true))-->output:boolean1.nil(空)
Lua將nil用來表示“無效值”。一個變數再第一次賦值前預設值是nil,將nil賦值給一個全域變數就等同於刪除它。
local numprint(num)-->output:nilnum = 100print(num)-->output:100
num = nil print(num) -->output:nil
2.boolean(布爾)
布爾值可選值為true/false,在Lua中nil和false為“假”,其餘都為“真”,比如0和Null 字元都為真。
3.number(數字)
number用於表示實數,可以使用數學函數math.floor(向下取整),math.ceil(向上取整)
localorder=3.0localscore=98.5print(math.floor(order))-->output:3print(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:helloworldprint(str2)-->output:helloluaprint(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 + yendlocal a = foo--把函數賦給變數print(a())--output:in the function30
Lua學習----Lua基礎資料類型