lua table操作函数

目录

 

lua_getfield

lua_setfield

lua_gettable

lua_settable

lua_rawget

lua_rawset

lua_rawgeti

lua_rawseti

区别


lua_getfield

  1. 原型:void lua_getfield (lua_State *L, int index, const char *k);
  2. 解释:把 t[k] 值压入堆栈 。

用于获取table中的键值,键k固定为字符串

lua_getglobal(L, "mytable") <== push mytable

lua_getfield(L, -1, "x")   <== push mytable["x"],作用同下面两行调用

const char *value= lua_tostring(L, -1) 如果是字符串的值,获取方式

lua_setfield

  1. 原型:void lua_setfield (lua_State *L, int index, const char *k);
  2. 解释:这个函数将把这个值弹出堆栈

设置table的键值,键K固定为字符串。设置后建和值都被自动移除堆栈

ua_getglobal(L, "mytable") <== push mytable

lua_pushstring(L, "abc")   <== push value "abc"

lua_setfield(L, -2, "x")   <== mytable["x"] = "abc", pop value "abc"

lua_gettable

  1. 原型:void lua_gettable (lua_State *L, int index);
  2. 解释:将值推到堆栈上,在给定有效索引中值在哪里,在堆栈顶部推入值。t[k]tk

用于获取table中的键值,键k可以为任意类型,键优先压入堆栈

此功能从堆栈中弹出密钥,值在栈顶

lua_getglobal(L, "mytable") <== push mytable

lua_pushnumber(L, 1)       <== push key 1

lua_gettable(L, -2)        <== pop key 1, push mytable[1]

lua_settable

  1. 原型:void lua_settable (lua_State *L, int index);
  2. 解释:作一个等价于 t[k] = v 的操作, 这里 t 是一个给定有效索引 index 处的值, v 指栈顶的值, 而 k 是栈顶之下的那个值。这个函数会把键和值都从堆栈中弹出

lua_getglobal(L, "mytable") <== push mytable

lua_pushnumber(L, 1)       <== push key 1

lua_pushstring(L, "abc")   <== push value "abc"

lua_settable(L, -3)        <== mytable[1] = "abc", pop key & value

lua_rawget

用法同lua_gettable,但更快(因为当key不存在时不用访问元方法__index)

lua_rawset

用法同lua_settable,但更快(因为当key不存在时不用访问元方法__newindex)

lua_rawgeti

必须为数值键

lua_getglobal(L, "mytable") <== push mytable

lua_rawgeti(L, -1, 1)      <== push mytable[1],作用同下面两行调用

--lua_pushnumber(L, 1)     <== push key 1

--lua_rawget(L,-2)         <== pop key 1, push mytable[1]

lua_rawseti

必须为数值键

lua_getglobal(L, "mytable") <== push mytable

lua_pushstring(L, "abc")   <== push value "abc"

lua_rawseti(L, -2, 1)      <== mytable[1] = "abc", pop value "abc"

堆栈顺序是table在栈低方向,依次时key,value

区别

 

获取

设置

 

直接操作键,键不需要压堆栈

lua_getfield

lua_setfield

键固定位字符串

lua_rawgeti

lua_rawseti

键固定位数值

键需要压堆栈,键类型不限定

 

lua_gettable

lua_settable

两者的区别没测,未知

lua_rawget

lua_rawset

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lua中的table是一种非常重要的数据结构,可以用来存储和组织数据。在Lua中,table可以被看作是一种关联数组,它可以通过任意类型的值作为索引来访问和操作其中的元素。 在Lua中,我们可以使用不同的方式来循环遍历table的元素。下面是几种常见的循环方式: 1. 使用pairs函数循环遍历table的键值对: ```lua local myTable = {key1 = value1, key2 = value2, key3 = value3} for key, value in pairs(myTable) do print(key, value) end ``` 这种方式会遍历table中所有的键值对,并将键和值分别赋值给变量key和value。 2. 使用ipairs函数循环遍历table的数组部分: ```lua local myTable = {"apple", "banana", "orange"} for index, value in ipairs(myTable) do print(index, value) end ``` 这种方式适用于table中只包含连续整数作为索引的情况,它会遍历数组部分,并将索引和对应的值分别赋值给变量index和value。 3. 使用数字索引循环遍历table: ```lua local myTable = {10, 20, 30} for i = 1, #myTable do print(i, myTable[i]) end ``` 这种方式适用于table中只包含连续整数作为索引的情况,通过指定起始索引和结束索引的方式来循环遍历table。 4. 使用迭代器函数循环遍历table: ```lua local myTable = {key1 = value1, key2 = value2, key3 = value3} for key, value in pairs(myTable) do print(key, value) end ``` 这种方式使用了Lua中的迭代器函数,可以自定义遍历table的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值