lua的loadstring()不适用于表 [英] lua's loadstring() not working with tables

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

问题描述

我有一些文本,我正在尝试通过加载字符串加载它.以下作品:

  local m = loadstring("data = 5")() 

但是当数据是表时,它将无法正常工作,并显示错误试图调用nil"

  local m = loadstring("data = {1 = 10}")() 

解决方案

lua中的表声明要求将整数键放在方括号内

  data = {[1] =值,} 

方括号中的键值始终是允许的、有效的和可能的.如果您的密钥遵循以下模式,则可以跳过它: [A-Za-z _] [A-Za-z0-9 _] * (与lua中的有效变量名相同)

I have some text and I'm trying to load it via load string. The following works:

local m = loadstring("data = 5")()

But when the data is a table it doesn't work and gives the error "attempt to call a nil"

local m = loadstring("data = { 1 = 10}")()

解决方案

The table declaration in lua require integer keys to be put inside square brackets:

data = {
  [1] = value,
}

The enclosing of keys in square brackets is always allowed, valid and possible. It can be skipped iff your key follows the pattern: [A-Za-z_][A-Za-z0-9_]* (which is the same as a valid variable name in lua)

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

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