简体   繁体   English

使用C API订购lua表循环

[英]Ordered lua table looping using C API

Consider the following lua table: 考虑以下lua表:

foo = {
    bar1 = {a = 1, b = 2, c = "hello"},
    bar2 = {a = 5, b = 2, c = "bbq"},
    bar3 = {a = 76, b = 13, c = "pwnd"}
}

I am trying to iterate this table using the lua C API to retrieve the key names, bar1, bar2 and bar3. 我试图使用lua C API迭代此表来检索键名,bar1,bar2和bar3。 I used the lua_next(L, -2) function to iterate as suggested by many, but the problem is that it returns the elements in random order. 我使用lua_next(L, -2)函数按照许多人的建议迭代,但问题是它以随机顺序返回元素。 The order changes on each run. 每次运行时订单都会更改。

I use the following code: 我使用以下代码:

for( lua_pushnil(L); lua_next(L, -2) != 0; lua_pop(L, 1) )
{
    printf("%s\n", lua_tostring(L, -2));
}

Most of the time, the output is unordered, such as bar2 bar1 bar3 . 大多数情况下,输出是无序的,例如bar2 bar1 bar3 When lucky, it's ordered. 幸运的是,它是订购的。 Is there an easy way to loop the table keys in an ordered fashion? 是否有一种简单的方法以有序的方式循环表键? What would be the equivalent code as I use, but ordered? 什么是我使用的等效代码,但订购? Thanks! 谢谢!

Edit: 编辑:

I know I am using a map rather than an array. 我知道我使用的是地图而不是数组。 But in lua script we have ipairs which would work just fine for this case. 但是在lua脚本中我们有ipairs,这对于这种情况会很好。 I'm looking at the C API equivalent. 我正在寻找C API等价物。 I've found this stackoverflow answer which gives a different answer, but it doesn't work for me, so I wonder if that answer is good or relevant. 我发现这个stackoverflow答案提供了不同的答案,但它对我不起作用,所以我想知道答案是好还是相关。

No. The order that Lua traverses tables is undefined. 不.Lua遍历表的顺序是未定义的。

If you know that the keys are of the form barXXX , then you can create these keys as strings dynamically in the order you wish and then get the corresponding value from the table. 如果您知道键的形式为barXXX ,那么您可以按照您希望的顺序动态创建这些键作为字符串,然后从表中获取相应的值。

Put the names in a C array, and then order them in C. 将名称放在C数组中,然后在C中对它们进行排序。

As others are saying, the order of keys returned by the hash portion of a table is not guaranteed. 正如其他人所说,不保证表的哈希部分返回的键的顺序。

Alternatively, use an array-like structure so you can use the array part: 或者,使用类似数组的结构,以便您可以使用数组部分:

foo = {
  {name = 'bar1', a = 1,  b = 2,  c = "hello"},
  {name = 'bar2', a = 5,  b = 2,  c = "bbq"},
  {name = 'bar3', a = 76, b = 13, c = "pwnd"}
}

So you can use ipairs or the equivalent to for i=1,#foo to iterate over the items in order. 因此,您可以使用ipairs或等效for i=1,#foo按顺序迭代项目。

Checking Lua's documentation, the main structure that Lua supports is a Hash Table . 检查Lua的文档, Lua支持的主要结构是哈希表 Given that it's always going to be a hash-table, you'd probably want to reimplement foo as an array . 鉴于它总是一个哈希表,你可能想要将foo重新实现为数组 The bar1 , bar2 , etc can be part of the entry into the array like so: bar1bar2等可以是进入数组的一部分,如下所示:

foo = {}
foo[0] = {name='bar1', ...}
foo[1] = {name='bar2', ...}
    ...

or as @lhf suggested, just build the bar names inside a for-loop (if you know it's in sequence) and retrieve the values from foo table. 或者@lhf建议,只需在for循环中构建bar名称(如果你知道它是按顺序)并从foo表中检索值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM