lua表寻址 [英] lua table addressing

查看:144
本文介绍了lua表寻址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子logTable = {["cdm"] = "log text", ["data_engine"] = "log text"}

代码:

for k,v in pairs(logTable) do
    print(v["cdm"]) end

给出理想的结果:日志文本

Gives the desired result: log text

但是

print(logTable["cdm"])

不返回任何内容.我在这里做什么错了?

returns nothing. What am I doing wrong here?

所以这实际上是我制作桌子的方式:

So this is actually how I am making the table:

table.insert(logTable, { [probeName] = file.read('export/'..robotName..'/probes/'..probeGroup..'/'..probeName..'/'..probeName..'.log')})    

probeName是一个变量,可能是我需要["probeName"]使其成为字符串吗?但是,这将其视为文字而不是变量吗?

Where probeName is a variable, could it be that I need to ["probeName"] to make it a string? However this takes it as a literal and not a variable?

如何使用变量作为表索引/键

How can I use a variable as table index/key

推荐答案

您实际上在此处的表 中有一个表,因此您的表如下所示:

You actually have a table inside a table here, so your table looks like this:

{
    { 
      ["cdm"] = "log text",
      ["data engine"] = "log text"
    }
}

这就是为什么您的print(logTable["cdm"])无法正常工作的原因. table.insert也应与数字索引表一起使用;这次,字典语法更清晰了:

That's why your print(logTable["cdm"]) doesn't work. table.insert is also meant to be used with number-indexed tables; this time, the dictionary syntax is clearer:

logTable["probeName"] = file.read('export/'..robotName..
                                  '/probes/'..probeGroup..'/'
                                  ..probeName..'/'..probeName..'.log')

这篇关于lua表寻址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆