• Lua table库整理(v5.1)


    这个库提供了表处理的通用函数。 所有函数都放在表 table。
    无论何时,若一个操作需要取表的长度, 这张表必须是一个真序列。

    table.concat(list, [, sep, [, i , [, j]]])

    提供一个列表,其所有元素都是字符串或数字,返回字符串 list[i]..sep..list[i+1] ··· sep..list[j]。 sep 的默认值是空串, i 的默认值是 1 , j 的默认值是 #list 。 如果 i 比 j 大,返回空串。
    sep为元素之间的间隔符

    local testTab = {1,2,3,4,5,6,7}
    print(table.concat(testTab))
    print (table.concat(testTab, "*", 1,3))
    
    Output:
    1234567
    1*2*3
    

    table.insert(table, [pos,] , value)

    在 list 的位置 pos 处插入元素 value , 并后移元素 list[pos], list[pos+1], ···, list[#list] 。 pos 的默认值为 #list+1 , 因此调用 table.insert(t,x) 会将 x 插在列表 t 的末尾。

    function printTable(tab)
        local output = ""
        for i,v in ipairs(tab) do
            output = output..v.."  "
        end
        print(output)
    end
    local testTab = {1,2,3,4}
    table.insert(testTab, 5)
    printTable(testTab)
    table.insert(testTab,2,10)
    printTable(testTab)
    table.insert(testTab, 8, 1)
    printTable(testTab)
    
    Output:
    1  2  3  4  5
    1  10  2  3  4  5
    1  10  2  3  4  5
    

    table.maxn(table)

    函数返回指定table中所有正数key值中最大的key值. 如果不存在key值为正数的元素, 则返回0。 此函数不限于table的数组部分

    local tab = {1,2,3,4}
    tab[100] = 2
    print(table.maxn(tab))
    tab[192.1] = 10
    print(table.maxn(tab))
    print(#tab)
    
    Output:
    100
    192.1
    4
    

    ** lua 5.3中被移除 **


    table.remove(table [, pos])

    移除 list 中 pos 位置上的元素,并返回这个被移除的值。 当 pos 是在 1 到 #list 之间的整数时, 它向前移动元素 list[pos+1], list[pos+2], ···, list[#list] 并删除元素 list[#list]; 索引 pos 可以是 #list + 1 ,或在#list 为 0 时可以是 0 ; 在这些情况下,函数删除元素 list[pos]。

    pos 默认为 #list, 因此调用 table.remove(l) 将移除表 l 的最后一个元素。

    function printTable(tab)
        local output = ""
        for i,v in ipairs(tab) do
            output = output..v.."  "
        end
        print(output)
    end
    local tab = {1,2,3,4,5,6,7}
    
    print(table.remove(tab))
    printTable(tab)
    
    print(table.remove(tab,2))
    printTable(tab)
    
    Output:
    7
    1  2  3  4  5  6
    2
    1  3  4  5  6
    

    table.sort(table, [, comp])

    在表内从 list[1] 到 list[#list] 原地 对其间元素按指定次序排序。 如果提供了 comp , 它必须是一个可以接收两个列表内元素为参数的函数。 当第一个元素需要排在第二个元素之前时,返回真 (因此 not comp(list[i+1],list[i]) 在排序结束后将为真)。 如果没有提供 comp, 将使用标准 Lua 操作 < 作为替代品。

    排序算法并不稳定; 即当两个元素次序相等时,它们在排序后的相对位置可能会改变。

    function printTable(tab)
        local output = ""
        for i,v in ipairs(tab) do
            output = output..v.."  "
        end
        print(output)
    end
    local tab = {1,6,7,4,3,9}
    table.sort(tab)
    printTable(tab)
    
    table.sort(tab, function(a,b) return a > b end)
    printTable(tab)
    
    Output:
    1  3  4  6  7  9
    9  7  6  4  3  1
    

    参考链接:http://cloudwu.github.io/lua53doc/manual.html

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Uva12657 Boxes in a Line
    Uva11988 Broken Keyboard (a.k.a. Beiju Text)
    Uva442 Matrix Chain Multiplication
    Uva514 Rails
    一些计划
    Java基础知识强化07:打印出空心菱形
    Java基础知识强化06:使用BigDecimal计算阶乘1+1/2!+1/3!+……
    Java基础知识强化05:不借助第三个变量实现两个变量互换
    Java基础知识强化04:判断101~200之间有多少素数
    Android(java)学习笔记144:网络图片浏览器的实现(ANR)
  • 原文地址:https://www.cnblogs.com/chiguozi/p/5797800.html
Copyright © 2020-2023  润新知