这个库提供了表处理的通用函数。 所有函数都放在表 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

Lua table库整理(v5.1)的更多相关文章

  1. Lua字符串库(整理)

    Lua字符串库小集 1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回 ...

  2. Lua string库整理

    string库提供了字符串处理的通用函数. 例如字符串查找.子串.模式匹配等. 当在 Lua 中对字符串做索引时,第一个字符从 1 开始计算(而不是 C 里的 0 ). 索引可以是负数,它指从字符串末 ...

  3. lua table库

      整理自:http://www.cnblogs.com/whiteyun/archive/2009/08/10/1543139.html 1.table.concat(table, sep,  st ...

  4. Lua整理——table库

    table属性 table库是有一些辅助函数构成的,这些函数将table作为数组来操作. 当中.有对列表中插入和删除元素的函数,有对数组元素进行排序的函数.还有对链接一个数组中全部字符串的函数. 0. ...

  5. Lua 之table库

    标准table库 table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写,table.concat()函数列出参数中指定 ...

  6. ulua 路径小记 以及 lua require 机制整理

    ulua 路径小记 在学习ulua时,require模块的根路径可以为项目的Lua文件夹或者ToLua文件夹(Editor下),但是在package.path和package.cpath中并没有看到当 ...

  7. Lua标准库- 模块(Modules)

    Lua包库为lua提供简易的加载及创建模块的方法,由require.module方法及package表组成 1.module (name [, ···]) 功能:建立一个模块. module的处理流程 ...

  8. cocos2d-x lua table数据存储

    cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...

  9. cocos2d-x lua table与json的转换

    cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...

随机推荐

  1. bzoj 3530: [Sdoi2014]数数

    #include<cstdio> #include<iostream> #include<cstring> #define M 1509 #define MO 10 ...

  2. app上传到App Store的快捷方法及步骤

    跳过证书的申请及配置概要文件的设置, 现在根据已有的配置概要文件及发布证书开始: 1.先在Xcode上的PROJECT和TARGETS->Build Setting->Code Signi ...

  3. Hibernate+Oracle注解式完整实例

    MyEclipse10,新建Web Project,取名hibernate, jar包 1.Cat.java (实体类) package com.hibernate.bean; import java ...

  4. Coding源码学习第三部分(EaseStartView.m)

    首先接上篇的要做一个NSEnumerator 类的延展阅读. 枚举(NSEnumerator) (1)依附于集合类(NSArray,NSSet,NSDictionary),没有用来创建实例的接口. ( ...

  5. js判断是否是移动端 访问移动端网址

    1以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.ba ...

  6. Linux 内核常见宏定义

    我们在阅读Linux内核是,常见到这些宏 __init, __initdata, __initfunc(), asmlinkage, ENTRY(), FASTCALL()等等.它们定义在 /incl ...

  7. android学习之RadioButton和CheckBox

    移通152 余继彪 RadioBuuton是一个单选按钮,CheckBox是一个复选按钮 . RadioButton的使用 ,首先要将RadioButton放在RadioGroup中,RadioGro ...

  8. JS Math.max() 函数

    Math.max(a,b,...,x,y) -- 返回数个数字中较大的值 max是maximum的缩写,中文"最大量"的意思 max函数语法Math.max(a,b,...,x,y ...

  9. Promise学习

    转自:http://www.cnblogs.com/lvdabao/p/es6-promise-1.html 去年6月份, ES2015正式发布(也就是ES6,ES6是它的乳名),其中Promise被 ...

  10. <textarea>使用的时候发现的两个问题的总结

    在练习表单的过程中,使用<textarea>时,遇到2个问题: 1.文本开始前有好多空格. 原来的代码是这样的: <textarea row="20" col=& ...