这个库提供了表处理的通用函数。 所有函数都放在表 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. spring框架详解: IOC装配Bean

    1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. 构造方法实例化:(默认无参数) 静态工厂实例化: 实例工厂实例化: 无参数构造方法的实例化: <!-- 默认情况下使用 ...

  2. C++小项目:directx11图形程序(七):modelclass

    模型类是世界空间中的表示物体的类,那么他的所做的事就是加载模型,移动模型,渲染模型 modelclass.h #pragma once #include <d3d11.h> #includ ...

  3. Git版本库

    创建版本库:git init db 只要用git init db 就可以很容易创建一个空的Git版本库. Git版本库创建好之后,在版本库的目录下有一个.git的子目录中有几项内容,其中注意三项: 1 ...

  4. Bpmx实施经验

    Bpmx是一个较大的平台,直接发布的话会有内存问题,经查阅一些资料,java1.5没有解决好之前版本的历史问题,所以在垃圾处理gc上有很多配置需要手动完成,之后的版本同上. Bpmx平台自带的文档中部 ...

  5. windows系统c盘占满/linux系统磁盘block、inode占满处理

    windows系统 下载c盘清理.bat到服务器,双击bat文件将自动清理 linux系统 先远程ssh登录上服务器,登录教程:http://www.west263.com/faq/list.asp? ...

  6. Sprint 2(第一天)

    Sprint 2计划会议: 目标: 1.实现用户模块的权限控制,能够进行用户登录的功能 2.对菜单模块实现增加菜单列表详情,修改菜单列表详情,删除菜单列表详情,查询菜单列表详情的功能 3.实现菜品分类 ...

  7. php 画图片2

    <?php // 使用php操作gd库做图 // 1. 创建一个画布资源 $im = imagecreatetruecolor(200, 50); // 2. 创建背景色 // 2.1 得到背景 ...

  8. Webdriver设置Chrome属性

    1. ChromeDriver加载插件 File file = new File ("files\\youtube.crx"); ChromeOptions options = n ...

  9. 【JavaScript】之【Object】

    见代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  10. ruby on rails 安装

    第一种方案: 1. 下载ruby    Ruby21-x64 2. 1 gem sources --remove http://rubygems.org 2. 2 gem sources -a htt ...