如何将文本文件加载到Lua中的类表变量中?
我需要将文件加载到 Lua 的变量中。
假设我得到了
name address email
There are space between every 。我需要将其中包含许多此类行的文本文件加载到某种对象中 - 或者至少应将一行剪切为由空格分隔的字符串数组。
这种工作在 Lua 中可行吗?我应该怎么做?我对 Lua 很陌生,但我在互联网上找不到任何相关内容。
I need to load file to Lua's variables.
Let's say I got
name address email
There is space between each. I need the text file that has x-many of such lines in it to be loaded into some kind of object - or at least the one line shall be cut to array of strings divided by spaces.
Is this kind of job possible in Lua and how should I do this? I'm pretty new to Lua but I couldn't find anything relevant on Internet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想阅读有关 Lua 模式 的内容,它们是 < a href="http://www.lua.org/pil/20.html" rel="noreferrer">字符串库。下面是一个示例函数(未测试):
该函数仅获取由非空格 (
%S
) 字符组成的三个子字符串。真正的函数会进行一些错误检查以确保模式确实匹配。You want to read about Lua patterns, which are part of the string library. Here's an example function (not tested):
This function just grabs three substrings made up of nonspace (
%S
) characters. A real function would have some error checking to make sure the pattern actually matches.扩展 uroc 的答案:
但这不包括您的名字中含有空格的情况。
To expand on uroc's answer:
This however won't cover the case that your names have spaces in them.
如果您可以控制输入文件的格式,则最好将数据存储为 Lua 格式,如下所述 这里。
如果没有,请使用 io 库 打开文件,然后使用 字符串库例如:
If you have control over the format of the input file, you will be better off storing the data in Lua format as described here.
If not, use the io library to open the file and then use the string library like: