Lua table concat


【1】table concat 簡介

使用方式:

table.concat(table, sep, start, end)

作用簡介:

concat是concatenate(連鎖、連接)的縮寫。

table.concat()函數列出指定table的數組部分從start位置到end位置的所有元素,元素間以指定的分隔符(sep)隔開。

除了table外,其余參數都不是必需的:

sep分隔符的默認值是空字符, start的默認值是1,end的默認值是數組部分的總長。

雖然sep, start, end都不是必需參數,但需明確實參賦值先后順序機制與C語言的類似,即若指定靠后的參數值, 必須同時指定前面的參數。

【2】學習示例

(2.1)數字下標連續(數組table 與列表table

-- 數字下標連續
-- tabTemp1
local tabTemp1 = 
{ 
    "c",
    "c++",
    "lua",
    "kotlin",
    "python",
    "go",
    "sql",
    "php"
};

print("length1: " .. (#tabTemp1))
print(table.concat(tabTemp1, ";"))

-- tabTemp2
local tabTemp2 = 
{ 
    [1] = "c",
    [2] = "c++",
    [3] = "lua",
    [4] = "kotlin",
    [5] = "python",
    [6] = "go",
    [7] = "sql",
    [8] = "php"
};

print("length2: " .. (#tabTemp2))
print(table.concat(tabTemp2, ";"))

-- tabTemp3
local tabTemp3 = 
{ 
    "c",
    "c++",
    "lua",
    a = 10,
    b = 20,
    "kotlin",
    "python",
    "go",
    "sql",
    "php"
};

print("length3: " .. (#tabTemp3))
print(table.concat(tabTemp3, ";"))

-- tabTemp4
local tabTemp4 = 
{ 
    "c",
    "c++",
    "lua",
    a = 10,
    b = 20,
    "kotlin",
    "python",
    "go",
    "sql",
    "php",
    [9] = "java",
    [10] = "swift"
};

print("length4: " .. (#tabTemp4))
print(table.concat(tabTemp4, ";"))\

--[[
length1: 8
c;c++;lua;kotlin;python;go;sql;php
length2: 8
c;c++;lua;kotlin;python;go;sql;php
length3: 8
c;c++;lua;kotlin;python;go;sql;php
length4: 10
c;c++;lua;kotlin;python;go;sql;php;java;swift
--]]

說明:

[1] 根據table的原理,其實,tabTemp1和tabTemp2本質是同一個table表,所以結果是相同的。

[2] table為數組或者是下標為1開始的有序列表時,說明concat方法操作一切正常。

(2.2)下標不連續(其他)

-- 數字下標不連續
-- tabTemp1
local tabTemp1 = 
{ 
    "c",
    "c++",
    "lua",
    a = 10,
    "119",
    "120",
    [6] = "python",
    [8] = "go",
    [15] = "sql",
    [19] = "end"
};

print("length1: " .. (#tabTemp1))
print(table.concat(tabTemp1, ";"))

-- tabTemp2
local tabTemp2 = 
{ 
    "c",
    "c++",
    "lua",
    a = 10,
    "119",
    "120",
    [6] = "python",
    [10] = "go",
    [15] = "sql",
    [19] = "end"
};

print("length2: " .. (#tabTemp2))
print(table.concat(tabTemp2, ";"))

-- tabTemp3
local tabTemp3 = 
{ 
    "c",
    "c++",
    "lua",
    a = 10,
    "119",
    "120",
    [3] = "python",
    [15] = "sql"
};

print("length3: " .. (#tabTemp3))
print(table.concat(tabTemp3, ";"))

-- tabTemp4
local tabTemp4 =
{ 
    [2] = "c",
    "c++",
    "lua",
    a = 10,
    "119",
    "120",
    [6] = "python",
    [8] = "go",
    [15] = "sql",
    [19] = "end"
};

print("length4: " .. (#tabTemp4))
print(table.concat(tabTemp4, ";"))

--[[
length1: 6
c;c++;lua;119;120;python
length2: 6
c;c++;lua;119;120;python
length3: 5
c;c++;lua;119;120
length4: 4
c++;lua;119;120
--]]

(2.3)下標沒有從1開始

-- 數字小標不從1開始
-- tabTemp
local tabTemp =
{ 
    [2] = "c",
    a = 10,
    b = 20,
    [6] = "python",
    [8] = "go",
    [15] = "sql",
    [19] = "end"
};

print("length: " .. (#tabTemp))
print("concat ret: ".. table.concat(tabTemp, ";"))

--[[
length: 0
concat ret: 
--]]

如上實例,仔細分析。

【3】總結

(1)第三個參數end,即table的長度非常關鍵。

(2)concat函數操作的table都是一個數組或者列表,也就是下標必須從一開始的連續數列。

(3)當下標不是從1開始時,且沒有數組或列表元素時,concat連接結果為空。

 

Good Good Study, Day Day Up.

順序 選擇 循環 總結


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM