如何将文本文件加载到Lua中的类表变量中?

发布于 2024-08-14 05:42:13 字数 237 浏览 5 评论 0原文

我需要将文件加载到 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 技术交流群。

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

发布评论

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

评论(3

鸠魁 2024-08-21 05:42:13

您想阅读有关 Lua 模式 的内容,它们是 < a href="http://www.lua.org/pil/20.html" rel="noreferrer">字符串库。下面是一个示例函数(未测试):

function read_addresses(filename)
  local database = { }
  for l in io.lines(filename) do
    local n, a, e = l:match '(%S+)%s+(%S+)%s+(%S+)'
    table.insert(database, { name = n, address = a, email = e })
  end
  return database
end

该函数仅获取由非空格 (%S) 字符组成的三个子字符串。真正的函数会进行一些错误检查以确保模式确实匹配。

You want to read about Lua patterns, which are part of the string library. Here's an example function (not tested):

function read_addresses(filename)
  local database = { }
  for l in io.lines(filename) do
    local n, a, e = l:match '(%S+)%s+(%S+)%s+(%S+)'
    table.insert(database, { name = n, address = a, email = e })
  end
  return database
end

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.

情话已封尘 2024-08-21 05:42:13

扩展 uroc 的答案:

local file = io.open("filename.txt")
if file then
    for line in file:lines() do
        local name, address, email = unpack(line:split(" ")) --unpack turns a table like the one given (if you use the recommended version) into a bunch of separate variables
        --do something with that data
    end
else
end
--you'll need a split method, i recommend the python-like version at http://lua-users.org/wiki/SplitJoin
--not providing here because of possible license issues

但这不包括您的名字中含有空格的情况。

To expand on uroc's answer:

local file = io.open("filename.txt")
if file then
    for line in file:lines() do
        local name, address, email = unpack(line:split(" ")) --unpack turns a table like the one given (if you use the recommended version) into a bunch of separate variables
        --do something with that data
    end
else
end
--you'll need a split method, i recommend the python-like version at http://lua-users.org/wiki/SplitJoin
--not providing here because of possible license issues

This however won't cover the case that your names have spaces in them.

允世 2024-08-21 05:42:13

如果您可以控制输入文件的格式,则最好将数据存储为 Lua 格式,如下所述 这里

如果没有,请使用 io 库 打开文件,然后使用 字符串库例如:

local f = io.open("foo.txt")
while 1 do
    local l = f:read()
    if not l then break end
    print(l) -- use the string library to split the string
end

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:

local f = io.open("foo.txt")
while 1 do
    local l = f:read()
    if not l then break end
    print(l) -- use the string library to split the string
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文