【1】table concat 簡介
php
使用方式:java
table.concat(table, sep, start, end) python
做用簡介:c++
concat是concatenate(連鎖、鏈接)的縮寫。sql
table.concat()函數列出指定table的數組部分從start位置到end位置的全部元素,元素間以指定的分隔符(sep)隔開。swift
除了table外,其他參數都不是必需的:數組
sep分隔符的默認值是空字符, start的默認值是1,end的默認值是數組部分的總長。 函數
雖然sep, start, end都不是必需參數,但需明確實參賦值前後順序機制與C語言的相似,即若指定靠後的參數值, 必須同時指定前面的參數。學習
【2】學習示例lua
(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.
順序 選擇 循環 總結