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.

    顺序 选择 循环 总结

  • 相关阅读:
    我的知识库(4) java获取页面编码(Z)
    知识库(3)JAVA 正则表达式 (超详细)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts
    某人总结的《英语听力的技巧 》,挺搞的
    我的知识库(5)java单例模式详解
    构建可扩展程序
    SerialPort (RS232 Serial COM Port) in C# .NET
    Python学习笔记——String、Sequences
    UI题目我的答案
    jQuery学习系列学会操纵Form表单元素(1)
  • 原文地址:https://www.cnblogs.com/Braveliu/p/11345686.html
Copyright © 2011-2022 走看看