标签: lua-table

Reading Lua nested tables in C++

我正在创建一个将从Lua调用的C/C++函数.我的函数必须调用一个库函数,其签名是这样的:

void libFunction( int val1, int val2, tSETTINGS * pSettings );
Run Code Online (Sandbox Code Playgroud)

我给了这些C/C++结构.

typedef struct
{
  int cmd;
  int arg;
} tCOMMAND;
typedef struct
{
  int numberCommands;
  int id;
  tCOMMAND commands[1];
} tSETTINGS;
Run Code Online (Sandbox Code Playgroud)

也许我的想法在这方面都是错的,但是从Lua我这样称呼:

id   = 42
val1 = 1
val2 = 2
cmd1 = { 3, 4 }
cmd2 = { 5, 6 }
commands = { cmd1, cmd2 }
settings = { #commands, id, commands }
mycfunction( val1, val2, settings )
Run Code Online (Sandbox Code Playgroud)

我确信我仍然不理解从C++引用的Lua堆栈,因为我正在尝试的东西不起作用.我的解决方案能够检索val1,val2,#command和id,但是当我尝试检索命令[0]和命令[ 1 ]时,我分别得到{1,2}和{ 2,42 }.

我的C++基本上就是这样(对于这个样本我正在丢弃这些值).我已经检索了val1和val2:

int stkNdx …
Run Code Online (Sandbox Code Playgroud)

c c++ lua nested-table lua-table

1
推荐指数
1
解决办法
3063
查看次数

Lua用两个值对表排序?

所以我有下表:

servers = {"ProtectedMethod" = {name = "ProtectedMethod", visits = 20, players = 2}, "InjecTive" = {name = "InjecTive", visits = 33, players = 1}};
Run Code Online (Sandbox Code Playgroud)

如何将服务器表中的子表排序为一个新的数组,该数组首先基于玩家,然后根据访问次数排序,这意味着除非两个表对玩家的值相同,否则您不会按访问次数进行排序。

例如,如果将排序代码放入名为tableSort的函数中,则我应该能够调用以下代码:

sorted = sort();
print(sorted[1].name .. ": " sorted[1].players .. ", " .. sorted[1].visits); --Should print "ProtectedMethod: 2, 20"
print(sorted[2].name .. ": " sorted[2].players .. ", " .. sorted[2].visits); --Should print "InjecTive: 1, 33"
Run Code Online (Sandbox Code Playgroud)

TIA

sorting lua lua-table

1
推荐指数
1
解决办法
1201
查看次数

如何在lua中声明一个包含X元素的数组

有没有办法在不使用的情况下为大数组声明和保留空间table.insert?像Python中的东西:

a = [0]*10000
Run Code Online (Sandbox Code Playgroud)

或在C:

malloc(10000*sizeof(int))
Run Code Online (Sandbox Code Playgroud)

lua lua-table

1
推荐指数
1
解决办法
2357
查看次数

表中的 Lua 表

local coordTable = {
    {loc={{1447, -2287, 13}, {0, 0, 3}, {100, -2000, 13}}, colour={255, 255, 255}},
}
Run Code Online (Sandbox Code Playgroud)

你好,

我试图从 loc 中获取每个值。所以,对于 loc 中的每个表,我想要里面三个数字的值。我不确定我的解释是否正确。

有点像这个问题,但稍微复杂一些。我可以使用上面链接的问题中的相同方法获取 loc 中的第一个表,但不能获取其他表。

任何帮助,将不胜感激。谢谢你。

lua lua-table

1
推荐指数
1
解决办法
1305
查看次数

这个函数如何:input.nn.MSECriterion_updateOutput(self,input,target)工作(在Lua/Torch中)?

我有这个功能:

    function MSECriterion:updateOutput(input, target)
        return input.nn.MSECriterion_updateOutput(self, input, target)
    end
Run Code Online (Sandbox Code Playgroud)

现在,

   input.nn.MSECriterion_updateOutput(self, input, target)
Run Code Online (Sandbox Code Playgroud)

返回一个数字.我不知道它是怎么做到的.我已经在调试器中一步步走了,似乎这只是计算一个没有中间步骤的数字.

 input is a Tensor of size 1 (say, -.234). And the 

 nn.MSECriterion_updateOutput(self, input, target) looks like it is just the function MSECriterion:updateOutput(input, target).
Run Code Online (Sandbox Code Playgroud)

我对如何计算数字感到困惑.

我很困惑为什么甚至允许这样做.参数输入是一个张量,它甚至没有任何名为nn.MSE input.nn.MSECriterion_updateOutput的方法.

lua function lua-table torch

1
推荐指数
1
解决办法
544
查看次数

Lua中语法糖(冒号)的奇怪行为

function string.test(s)
    print('test')
end

a = 'bar'
string.test(a)
a:test()
Run Code Online (Sandbox Code Playgroud)

一切都很好,直到下一个例子.

function table.test(s)
    print('test')
end

b = {1,2,3}
table.test(b)
b:test() -- error
Run Code Online (Sandbox Code Playgroud)

为什么我收到错误?
它在琴弦上工作得很好.

lua function syntactic-sugar lua-table

1
推荐指数
1
解决办法
374
查看次数

Lua表和类有什么区别?

我是一般编程的初学者,我一直在尝试一些不同的语言.在Lua中,有些表似乎就像超级列表(数组,字典,列表一样)但在Lua中可以这样做:

player = { health = 100, attack = 50, mana = 54 }
print(player.health)
Run Code Online (Sandbox Code Playgroud)

它将返回100.但在其他编程语言中,您需要创建一个类来获得相同的输出.但根据我的理解,Lua有课程和桌子吗?但表似乎非常相似,所以它们是一样的吗?如果没有,是什么让它们与众不同,使用它们的利弊是什么?

arrays oop lua class lua-table

1
推荐指数
1
解决办法
731
查看次数

关于Lua的冒号操作员

为什么这段代码失败(尝试调用方法'sort'(一个零值))

th> xyz = {1,2,3}                                                                      
th> xyz:sort()
Run Code Online (Sandbox Code Playgroud)

这虽然有效

th> table.sort(xyz)
Run Code Online (Sandbox Code Playgroud)

oop lua lua-table

1
推荐指数
1
解决办法
274
查看次数

lua不修改函数参数

我一直在学习lua,似乎无法简单地实现这个二叉树的工作......

function createTree(tree, max)
    if max > 0 then
        tree = {data = max, left = {}, right = {}}
        createTree(tree.left, max - 1)
        createTree(tree.right, max - 1)
    end
end

function printTree(tree)
    if tree then
        print(tree.data)
        printTree(tree.left)
        printTree(tree.right)
    end
end

tree = {}
createTree(tree, 3)
printTree(tree)
Run Code Online (Sandbox Code Playgroud)

程序在执行后返回nil.我在网上搜索了解参数传递如何在lua中工作(如果它是通过引用或值)并且发现某些类型通过引用(如表和函数)传递,而其他类型通过值传递.尽管如此,我还是将全局变量"tree"作为一个表传递给了"createTree"函数,我甚至将"left"和"right"初始化为"createTree"中的空表,用于相同的目的.我究竟做错了什么?

lua binary-tree arguments function lua-table

1
推荐指数
2
解决办法
1144
查看次数

如何通过使用lua从数组中获取所有值

我有一个多维数组:

result = {
  {
    data = {
      language = "English",
      name = "Freak Out",
      list = {
        {
          type = "songs",
          album = "1234"
        }, {
          type = "songs",
          album = "4234"
        }, {
          type = "songs",
          album = "5829"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如何动态访问此数组中的列表?

此代码正在打印第一张相册(1234):

for i, v in pairs(result) do print(v.data.list[1].album) end
Run Code Online (Sandbox Code Playgroud)

我想album用它们打印所有的type。我该怎么做呢?

lua lua-table

1
推荐指数
1
解决办法
358
查看次数