Lua:在 for k,v inpair(tbl) 循环中进行算术运算

发布于 2024-12-18 03:47:24 字数 1355 浏览 2 评论 0原文

我有一个如下表:

mafiadb:{"Etzli":{"alive":50,"mafia":60,"vigilante":3,"doctor":4,"citizen":78,"police":40},"Charneus":{"alive":29,"mafia":42,"vigilante":6,"doctor":14,"citizen":53,"police":33}}

还有更多嵌套表,但我现在只是想保持简单。

我运行以下代码来提取某些值(我正在根据这些值制作一个有序列表):

sortmaf={}
for k,v in pairs(mafiadb) do
sortmaf[k]=v["mafia"]
end

这是我运行的代码之一。我遇到的问题是,您似乎无法在表循环中进行算术运算。我尝试过:

sortpct={}
for k,v in pairs(mafiadb) do
sortpct[k]=(v["alive"]*100)/(v["mafia"]+v["vigilante"]+v["doctor"]+v["citizen"]+v["police"])
end

它返回我正在尝试对“alive”字段进行算术运算。我在这里缺少什么?和往常一样,我感谢您在回答这个问题时所考虑的一切!

编辑: 我不会对评论进行评论,而是将在此处添加其他信息。

我发布的 mafiadb 数据库是真实的数据库。它只是被精简为两名球员,而不是我目前列出的 150 多名球员。它的结构很简单:

mafiadb = {
            Playername = {
                           alive = 0
                           mafia = 0
                           vigilante = 0
                           doctor = 0
                           police = 0
                           citizen = 0
                          }
           }

添加数百个玩家姓名,然后就完成了。

至于错误信息,确切的信息是:

尝试对字段“alive”执行算术(零值)

所以...我不确定问题是什么。在我的第一个代码中,使用 sortmaf 的代码,它工作得很好,但是突然间,当我尝试进行算术运算时,它找不到 v["alive"] 作为值?如果我只是将 v["alive"] 单独放置,它会突然被发现并且不再为零。我希望这能澄清一点。

I have a table such as the following:

mafiadb:{"Etzli":{"alive":50,"mafia":60,"vigilante":3,"doctor":4,"citizen":78,"police":40},"Charneus":{"alive":29,"mafia":42,"vigilante":6,"doctor":14,"citizen":53,"police":33}}

There are more nested tables, but I'm just trying to keep it simple for now.

I run the following code to extract certain values (I'm making an ordered list based on those values):

sortmaf={}
for k,v in pairs(mafiadb) do
sortmaf[k]=v["mafia"]
end

That's one of the codes I run. The problem I'm running into is that it doesn't appear you can do arithmetic in a table loop. I tried:

sortpct={}
for k,v in pairs(mafiadb) do
sortpct[k]=(v["alive"]*100)/(v["mafia"]+v["vigilante"]+v["doctor"]+v["citizen"]+v["police"])
end

It returns that I'm attempting to do arithmetic on field "alive." What am I missing here? As usual, I appreciate any consideration in answering this question!

Editing:
Instead of commenting on the comment, I'm going to add additional information here.

The mafiadb database I've posted IS the real database. It's just stripped down to two players instead of the current 150+ players I have listed in it. It's simply structured as such:

mafiadb = {
            Playername = {
                           alive = 0
                           mafia = 0
                           vigilante = 0
                           doctor = 0
                           police = 0
                           citizen = 0
                          }
           }

Add a few hundred more playernames, and there you have it.

As for the error message, the exact message is:

attempt to perform arithmetic on field 'alive' (nil value)

So... I'm not sure what the problem is. In my first code, the one with sortmaf, it works perfectly, but suddenly, it can't find v["alive"] as a value when I'm trying to do arithmetic? If I just put v["alive"] by itself, it's suddenly found and isn't nil any longer. I hope this clarifies a bit more.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2024-12-25 03:47:24

对我来说这看起来像是一个简单的错字。

您的 150 个字符中有一些写得不好 - 可能它们没有“活动”属性,或者写得不正确,或者不是数字。试试这个:

for k,v in pairs(mafiadb) do
  if type(v.alive) ~= 'number' then
    print(k, "doesn't have a correct alive property")
  end
end

这应该打印“坏”字符的名称。

This looks like a simple typo to me.

Some of your 150 characters is not well written - probably they don't have an "alive" property, or it's written incorrectly, or it's not a number. Try this:

for k,v in pairs(mafiadb) do
  if type(v.alive) ~= 'number' then
    print(k, "doesn't have a correct alive property")
  end
end

This should print the names of the "bad" characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文