lua的loadstring()不适用于表

编程入门 行业动态 更新时间:2024-10-13 04:19:40
本文介绍了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()不适用于表

本文发布于:2023-10-26 17:43:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   lua   loadstring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!