这个库提供了表处理的通用函数。 所有函数都放在表 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. 【软件工具】Driver Booster3永久激活法

    原作者網址:erik2041999 (YouTube) 1.安装Driver Booster3 (档案已附) 2.使用此启动码0187E-B9764-4D9FA-211B3断网启动 3.保持断网状态并 ...

  2. WebStorm调试node.js

    直接上图:

  3. Bootstrap<基础十一>字体图标(Glyphicons)

    字体图标(Glyphicons),并通过一些实例了解它的使用.Bootstrap 捆绑了 200 多种字体格式的字形. 获取字体图标 我们已经在 环境安装 章节下载了 Bootstrap 3.x 版本 ...

  4. bzoj 3172 单词 ac自动机|后缀数组

    题目大意: 给定n个字符串连成了一篇文章,问每个字符串在这篇文章中出现的次数,可重复覆盖 这里ac自动机和后缀数组都可以做 当然后缀数组很容易就解决,但是相对时间消耗高 这里就只讲ac自动机了 将每个 ...

  5. 控制台手动编译Qt5程序

    转自:http://www.cnblogs.com/csulennon/p/4479236.html 在上一篇随笔中已经搭建好了Qt5的的开发环境,并且通过Qt Creator自动构建了一个视窗程序. ...

  6. JSP 甜点

    JSP cookies Cookies是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookies的支持. 通常有三个步骤来识别回头客: ...

  7. 【其他】win7创建wifi热点共享给手机使用

    出门在外,有时候网络有诸多不便,需要用笔记本创建wifi热点给手机用:本人测试xp怎么配置都不好使,但win7有可行的方案,不依赖第三方软件. 详述如下: 场景一:win7 + A(PC机)(用无线连 ...

  8. python sqlite 插入的数据含有变量,结果不一致

    def insert(): conn = sqlite3.connect("sqlite.db") print "open database passed" t ...

  9. vs2012 检测到有潜在危险的 Request.Form 值

    今天用vs2012写网站,其中要用到网页编辑器,调试时提示:检测到有潜在危险的 Request.Form 值随即上网搜索: 解决方法一:具体页面添加 ValidateRequest="fal ...

  10. PKU 1005

    比较简单吧,其实算是数学问题了 // 1005.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdio.h ...