Lua温故知新篇1 表操作

table.concat (list [, sep [, i [, j]]])

-- table 方法
-- table.concat(list [, sep [, i [, j]]])
-- list:一个都为字符串或者字符的表
-- sep:分隔符号,默认空字符串
-- i:默认值1
-- j:默认值#list

local t = {
    
    'a','b','c'}
local str = table.concat(t)
print(str)
-- 'abc'

str = table.concat(t,"_")
print(str)
-- 'a_b_c'

str = table.concat(t,"_",1,2)
print(str)
-- 'a_b'

str = table.concat(t,"_",2,3)
print(str)
-- 'b_c'

str = table.concat(t,"_",2,2)
print(str)
-- 'b'

table.insert (list, [pos,] value)

-- table 方法
-- table.insert (list, [pos,] value)
-- pos:位置,默认值#list+1

local t = {
    
    
    [1] = 1,
    [2] = 2,
    [3] = 3,
}

-- 可以不传pos,默认在表的后端加入一个
table.insert(t, 4)
-- [1] = 1
-- [2] = 2
-- [3] = 3
-- [4] = 4

-- 在位置1插入一个,值为5。前面其他的自然会往后退一个
table.insert(t,1,5)
-- [1] = 5
-- [2] = 1
-- [3] = 2
-- [4] = 3
-- [5] = 4

table.insert(t,2,6)
-- [1] = 5
-- [2] = 6
-- [3] = 1
-- [4] = 2
-- [5] = 3
-- [6] = 4

table.move (a1, f, e, t [,a2])

-- table 方法
-- table.move (a1, f, e, t [,a2])
-- 将若干元素从a1移动到a2.
-- 从a1的f到e元素,移动到a2表的从t开始
-- a1:原来的表
-- a2:移去的表 [默认值是a1表]
-- 返回a2表

local t1 = {
    
    
    [1] = 'a',
    [2] = 'b',
    [3] = 'c',
}

local t2 = {
    
    
    [1] = 1,
    [2] = 2,
    [3] = 3,
    [4] = 4,
}

-- table.move(t1,1,1,3)
-- 第一个元素移动到第三位
-- t1:  [1] = "a"
--      [2] = "b"
--      [3] = "a"

-- table.move(t1,1,2,3)
-- 1到2位移动到第3位开始
-- t1:  [1] = "a"
--      [2] = "b"
--      [3] = "a"
--      [4] = "b"


-- table.move(t1,1,2,3,t2)
-- t1的1到2位移动到t2第3位开始
-- t2:  [1] = "1"
--      [2] = "2"
--      [3] = "a"
--      [4] = "b"

-- t2 = {}
-- table.move(t1,1,2,3,t2)
-- 置空了t2表,t1的1到2位移动到t2第3位开始。1、2为空,3开始填
-- t2:  [1] = 
--      [2] = 
--      [3] = "a"
--      [4] = "b"


-- t1[2] = nil
-- table.move(t1,1,2,3,t2)
-- 置空了t2表第二位,t1的1到2位移动到t2第3位开始。1、2为空,3开始填
-- t2:  [1] = 1
--      [2] = 2
--      [3] = "a"


t1[1] = nil
table.move(t1,1,2,3,t2)
-- 置空了t2表第一位,t1的1到2位移动到t2第3位开始。1、2为空,3开始填
-- t2:  [1] = 1
--      [2] = 2
--      [3] = "b"

table.remove (list [, pos])

-- table 方法
-- table.remove (list [, pos])
-- 移除在索引为pos的值,并返回它
-- pos 默认值是#list(pos可以为0,也可以大于#list,只不过是空,返回的value也是空)。
--     默认值是#list,就是移除最后一个.


local t = {
    
    
    [1] = 'a',
    [2] = 'b',
    [3] = 'c',
}

-- 移除索引为1的数据
local value = table.remove(t,1)
print(value)
-- t: [1] = 'b',[2] = 'c',
-- value:a

-- 移除0位置
value = nil
value = table.remove(t,0)
print(value)

-- 移除#list+1位置
value = nil
value = table.remove(t,#t + 1)
print(value)

table.pack (···)

-- table 方法
-- table.pack (···)
-- 返回一个新表,其中所有参数都存储在键 1、2 等中,并带有一个字段“ n”,其中包含参数总数。请注意,如果某些参数为nil,则结果表可能不是序列。

local function func(...)
    local params = table.pack(...)
    -- params: [1] 到 [(#t)]是传入的内容,[n]里是传入的内容的长度

end

func('a','b','c')
-- params:  [1] = 'a'
--          [2] = 'b'
--          [3] = 'c'
--          [n] = 3

func('a','b','c',5)
-- params:  [1] = 'a'
--          [2] = 'b'
--          [3] = 'c'
--          [4] = 5
--          [n] = 4

-- 传入空值,也会占一个位置,但是传入的值数量n不变
func('a','b','c',nil,5)
-- params:  [1] = 'a'
--          [2] = 'b'
--          [3] = 'c'
--          [4] = nil
--          [5] = 5
--          [n] = 5

-- 传入空值,也会占一个位置,但是传入的值数量n不变
func('a','b','c',5,nil)
-- params:  [1] = 'a'
--          [2] = 'b'
--          [3] = 'c'
--          [4] = 5
--          [5] = nil
--          [n] = 5

-- 传入全部空值,也会占位置,但是传入的值数量n不变
func(nil,nil)
-- params:  [1] = nil
--          [2] = nil
--          [n] = 2

table.unpack (list [, i [, j]])

-- table 方法
-- table.unpack (list [, i [, j]])
-- 返回给定列表中的元素。这个函数相当于返回列表[i]、列表[i+1]、···、列表[j]。默认情况下,i是 1 , j是#list。

local function func(t)
    local t1,t2,t3 = table.unpack(t)

end


local t = {
    
    
    [1] = 'a',
    [2] = 'b',
    [3] = 'c'
}


func(t)
-- t1 = 'a'
-- t2 = 'b'
-- t3 = 'c'


t = {
    
    
    [1] = 'a',
    [2] = 'b',
    [4] = 'c'
}
func(t)
-- 序列访问,空/不连续停止
-- t1 = 'a'
-- t2 = 'b'
-- t3 = nil
-- t4 = nil

table.sort (list [, comp])

-- table 方法
-- table.sort (list [, comp])
-- [默认1~n升序,对应'<']
-- [返回true的,index越小]
-- [无返回值]
-- [sort并不是可靠排序]
-- [table必须从1开始]
-- 按给定顺序对列表元素进行就地 排序,从list[1]到list[#list]。如果comp给出,那么它必须是一个接收两个列表元素的函数,
-- 并且当第一个元素在最终顺序中必须在第二个元素之前时返回 true,因此在排序之后, i <= j暗示not comp(list[j],list[i]). 
-- 如果comp没有给出,则使用标准 Lua 运算符<。

-- comp函数必须定义一致的顺序 ;更正式地说,函数必须定义严格的弱顺序。(弱序类似于全序,但它可以将不同的元素等同起来以进行比较。)
-- 排序算法是不稳定的:给定顺序认为相等的不同元素的相对位置可能会因排序而改变。

local t = {
    
    
    [1] = 'a',
    [2] = 'c',
    [3] = 'b'
}

table.sort(t)
-- [1] = 'a',
-- [2] = 'b',
-- [3] = 'c'


t = {
    
    
    [1] = 3,
    [2] = 2,
    [3] = 1
}

table.sort(t)
-- [1] = 1,
-- [2] = 2,
-- [3] = 3

t = {
    
    
    [1] = 3,
    [2] = 5,
    [3] = 1,
    [5] = 2,
}

-- 对于断开的index,只会计算从1开始没断开的index
table.sort(t)
-- [1] = 1,
-- [2] = 3,
-- [3] = 5,
-- [5] = 2,


猜你喜欢

转载自blog.csdn.net/weixin_44293055/article/details/125362709