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 =
{
[] = "c",
[] = "c++",
[] = "lua",
[] = "kotlin",
[] = "python",
[] = "go",
[] = "sql",
[] = "php"
}; print("length2: " .. (#tabTemp2))
print(table.concat(tabTemp2, ";")) -- tabTemp3
local tabTemp3 =
{
"c",
"c++",
"lua",
a = ,
b = ,
"kotlin",
"python",
"go",
"sql",
"php"
}; print("length3: " .. (#tabTemp3))
print(table.concat(tabTemp3, ";")) -- tabTemp4
local tabTemp4 =
{
"c",
"c++",
"lua",
a = ,
b = ,
"kotlin",
"python",
"go",
"sql",
"php",
[] = "java",
[] = "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 = ,
"",
"",
[] = "python",
[] = "go",
[] = "sql",
[] = "end"
}; print("length1: " .. (#tabTemp1))
print(table.concat(tabTemp1, ";")) -- tabTemp2
local tabTemp2 =
{
"c",
"c++",
"lua",
a = ,
"",
"",
[] = "python",
[] = "go",
[] = "sql",
[] = "end"
}; print("length2: " .. (#tabTemp2))
print(table.concat(tabTemp2, ";")) -- tabTemp3
local tabTemp3 =
{
"c",
"c++",
"lua",
a = ,
"",
"",
[] = "python",
[] = "sql"
}; print("length3: " .. (#tabTemp3))
print(table.concat(tabTemp3, ";")) -- tabTemp4
local tabTemp4 =
{
[] = "c",
"c++",
"lua",
a = ,
"",
"",
[] = "python",
[] = "go",
[] = "sql",
[] = "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 =
{
[] = "c",
a = ,
b = ,
[] = "python",
[] = "go",
[] = "sql",
[] = "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.
顺序 选择 循环 总结
Lua table concat的更多相关文章
- 【本·伍德Lua专栏】补充的基础09:使用table.concat将一个大的字符串
近期2天都没有写新的文章了.主要是近期的内容没有特别有意思的. 之前的协同程序也临时没有感觉到特别适用的地方.今天在看数据结构的部分,也是没多大意思(不代表没用). 但是突然发现了一个有意思的地方,那 ...
- LUA table学习笔记
function printT( ... ) for i,v in ipairs(...) do print(i,v) end end t1={} t2={} t3={} table.insert(t ...
- Lua table库整理(v5.1)
这个库提供了表处理的通用函数. 所有函数都放在表 table. 无论何时,若一个操作需要取表的长度, 这张表必须是一个真序列. table.concat(list, [, sep, [, i , [, ...
- 树形打印lua table表
为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...
- Lua table使用
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...
- lua table表
lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...
- Lua 学习之基础篇四<Lua table(表)>
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- lua table 的操作(四)
table在前面作过介绍,它是一种关联数组,这种关联指的是可以设置各类类型的key来存储值. 1.table 间的数据传递 -- 为 table a 并设置元素,然后将 a 赋值给 b,则 a 与 b ...
- Lua table(表)
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
随机推荐
- getOwnPropertyDescriptor
语法 Object.getOwnPropertyDescriptor(obj, prop) 参数 obj 需要查找的目标对象 prop 目标对象内属性名称 返回值 如果指定的属性存在于对象上,则返回其 ...
- (原)ubuntu中C++调用libotrch
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11479240.html 参考网址: https://pytorch.org/tutorials/ad ...
- Odoo search 搜索视图详解与搜索视图工作原理
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826430.html 搜索视图 搜索视图的search标签本身没什么属性可以使用,只要是<searc ...
- nginx常用伪静态设置
nginx里使用伪静态是直接在nginx.conf 中写规则的,并不需要像apache要开启写模块(mod_rewrite)才能进行伪静态. nginx只需要打开nginx.conf配置文件,在ser ...
- OpenCV 学习笔记(14)为轮廓创建边界旋转框和椭圆
https://docs.opencv.org/3.4/de/d62/tutorial_bounding_rotated_ellipses.html 不旋转 #include "opencv ...
- LeetCode 691. Stickers to Spell Word
原题链接在这里:https://leetcode.com/problems/stickers-to-spell-word/ 题目: We are given N different types of ...
- Pandas | 08 重建索引
重新索引会更改DataFrame的行标签和列标签. 可以通过索引来实现多个操作: 重新排序现有数据以匹配一组新的标签. 在没有标签数据的标签位置插入缺失值(NA)标记. import pandas a ...
- docker自定义镜像上传阿里云
1.alpine制作jdk镜像 2.Alpine制作jre镜像(瘦身) 3.Docker镜像上传至阿里云 alpine制作jdk镜像 alpine Linux简介 1.Alpine Linux是一个轻 ...
- 网络协议 4 - 交换机与 VLAN:拓扑结构
上一次,我们通过宿舍联网打魔兽的需求,认识了如何通过物理层和链路层组建一个宿舍局域网.今天,让我们切换到稍微复杂点的场景,办公室. 在这个场景里,就不像在宿舍那样,搞几根网线,拉一拉,扯一扯就 ...
- centos7 编译安装 haproxy1.8.20
当前系统信息: [root@localhost ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core) ...