zoukankan      html  css  js  c++  java
  • 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.

    顺序 选择 循环 总结

  • 相关阅读:
    [原创] 扩展jquery-treegrid插件, 实现勾选功能和全删按钮.
    [原创]多版本Java环境变量的配置
    [转]Redmine 配置163邮箱
    [转] --- Error: “A field or property with the name was not found on the selected data source” get only on server
    服务器控件中使用<%#...>, JS和html控件中使用<%=...>
    【字源大挪移—读书笔记】 第三部分:字尾
    【字源大挪移—读书笔记】 第二部分:字根
    使用WebClient 或者 HttpWebRequest均报:"The Remote name can't be solved"
    【字源大挪移—读书笔记】 第一部分:字首
    【英语魔法俱乐部——读书笔记】 3 高级句型-简化从句&倒装句(Reduced Clauses、Inverted Sentences) 【完结】
  • 原文地址:https://www.cnblogs.com/Braveliu/p/11345686.html
Copyright © 2011-2022 走看看