lua table 初识

 
 1 local demo = {"demoValue"}
 2 local function fun()
 3   print"hello world"
 4 end
 5 local tableDemo = {
 6             ["a3132t"] = "valide?",
 7                     [9] = fun,
 8                     [fun] = "a funciton.",
 9                     ["demo"] = "anoter string",
10                     demo = "a string",            --Syntactic sugar
11                     ["demo"] = "come on",
12                     "here to be count!",
13                     [demo] = "a table demo to be anoter table‘s key",
14                     [1]="demo",
15                     "world" ,
16                     "apple",
17                     "helloworld",
18                 }
1.table中所有的元素都以逗号分隔;2.table中所有的元素都要添加索引——以中括号"["和"]" 括起来;  2.1.如果不使用中括号,合法的元素会以数字为索引,且按顺序自动从加1往后递增;  2.2.如果没有使用"["和"]" 括起来,且合法,则认为是字符串索引(Syntactic sugar);

值得注意的是保存着table和function的变量既可以做索引也可以做值。只不过做索引的依然需要遵循以上规则。以table demo为例,如果没有使用"["和"]" 括起来,那么tableDemo则认为demo只是一个tableDemo元素的key,它实质上字符串"demo"。
1 for k,v in pairs(tableDemo) do
2   print(k,v)
3 end

标准输出如下:

 1 1 here to be count!
 2 2    world
 3 3    apple
 4 4    helloworld
 5 5    function: 0x172c730
 6 9    function: 0x172c700
 7 a3132t    valide?
 8 table: 0x172cde0    a table demo to be anoter table‘s key
 9 demo    come on
10 function: 0x172c700    a funciton.
11 [Finished in 0.0s]

待续的主题还有:

1.标准库函数如

1 table.sort (table [, comp]),
2 table.insert (table, [pos,] value)
3 table.concat (table [, sep [, i [, j]]])
4 table.remove (table [, pos])

2.标准库字符串操作函数

时间: 2024-08-29 14:28:56

lua table 初识的相关文章

lua table integer index 特性

table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.) 返回表中最大的正数值index. 说明: 1. 此接口不是统计表中元素的

Lua table pair和ipair区别

官方描述: ipairs (t) Returns three values: an iterator function, the table t, and 0, so that the construction for i,v in ipairs(t) do body end will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table. pairs (

Lua table 的长度问题

直入主题 我们要取lua table的长度时习惯这样 local tb = {1,2,3,4} print(#tb) 这时候,输出理所当然的是4,如果在程序中我们需要判断长度时使用这样的方法,再看一下 tb[1] = nil print(#tb) 输出还是4,似乎有点背离我们的意愿 我们想知道这个table里面是不是有4个值,但是有一个值被置空了,输出结果却没有及时告诉我们 发生这样问题的原因是,lua在初始化table的时候,会给这个table分配值存储的空间,代码里面tb初始化包含4个数字值

树形打印lua table表

local print = print local tconcat = table.concat local tinsert = table.insert local srep = string.rep local type = type local pairs = pairs local tostring = tostring local next = next function print_lua_table (lua_table, indent) if not lua_table or t

(转) 关于lua table是否为空的判断

在项目的脚本lua中经常有这样的需求, 1.local a = {} 2.对a进行处理 3.对a是否为空表进行判断 关于对a是否为空表的判断,我发现有些代码如此做: if a == {} then 这样的结果就是a == {}永远返回false,是一个逻辑错误.因为这里比较的是table a和一个匿名table的内存地址. 也有些代码如此做: if table.maxn(a) == 0 then 这样做也不保险,除非table的key都是数字,而没有hash部分. 难道真的要遍历table发现有

Lua table转C++数组

为了方便,这里数组用vector表示. C++层代码如下: static int LuaTable2Vector(lua_State *luaState) { int count = luaL_getn(luaState, 1); std::vector<std::string> vecNameList; for (int i = 0; i < count; i++) { lua_rawgeti(luaState, 1, i+1); const char *strName = lua_t

从实现求差集介绍lua table需要注意的一些问题

用lua实现的求两个table的差集(只支持一维table) 1.lua table 判空: table 判空,用的了next()函数. next()函数说明:运行程序来遍历表中的所有域. 第一个参数是要遍历的表,第二个参数是表中的某个键. next 返回该键的下一个键及其关联的值. 如果用 nil 作为 第二个参数调用 next 将返回初始键及其关联值. 当以最后一个键去调用,或是以 nil 调用一张空表时, next 返回 nil. 如果不提供第二个参数,将认为它就是 nil. 可以用 ne

Lua Table pairs输出顺序问题 (版本差异 解决数字索引间断并兼容字符串索引)

问题标签: Lua Table 迭代器;Lua Table 输出顺序; Lua Table 顺序输出;Lua Table 数字索引 字符串索引;Lua Table pairs; 问题背景: 使用pairs输出table时,其输出顺序与通常认知不相符. 例如使用pairs输出如下table T = { [1] = "1", [2] = "1", [3] = "1", [4] = "1", [5] = "1",

Lua table 顺序遍历

在Lua中, 如何遍历一个Table元素,主要有两种类型: 1.pairs:下标从1开始,并且是连续的才可以使用 2.ipairs:是无序的示例 local t = { [10] = 1, [20] = 2, [30] = 3, [40] = 4, [50] = 5, [60] = 6 } for k,v in pairs (t) do print("t1", k, v) end for k,v in ipairs (t) do print("t2", k, v)