Lua学习---c和lua的交互之表的操作


lua_gettable
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
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"

lua_getfield必须为字符串键
lua_getglobal(L, "mytable") <== push mytable
lua_getfield(L, -1, "x")    <== push mytable["x"],作用同下面两行调用
--lua_pushstring(L, "x")    <== push key "x"
--lua_gettable(L,-2)        <== pop key "x", push mytable["x"]

lua_setfield必须为字符串键
lua_getglobal(L, "mytable") <== push mytable
lua_pushstring(L, "abc")    <== push value "abc"
lua_setfield(L, -2, "x")    <== mytable["x"] = "abc", pop value "abc"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值