在Lua中解析文件的最有效方法 [英] Most efficient way to parse a file in Lua

查看:89
本文介绍了在Lua中解析文件的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出使用Lua从文件解析数据的最有效方法.例如,假设我有一个文件(example.txt),其中包含以下内容:

I'm trying to figure out what is the most efficient way to parse data from a file using Lua. For example lets say I have a file (example.txt) with something like this in it:

0, Data
74, Instance
4294967295, User
255, Time

如果我只希望数字在"之前,那么我可以想到几种获取信息的方法.我先从f = io.open(example.txt)获取数据,然后使用for循环解析f的每一行.这引出了我的问题的核心.最有效的方法是什么?

If I only want the numbers before the "," I could think of a few ways to get the information. I'd start out by getting the data with f = io.open(example.txt) and then use a for loop to parse each line of f. This leads to the heart of my question. What is the most efficient way to do this?

在for循环中,我可以使用以下任何一种方法在逗号前获取#号:

In the for loop I could use any of these methods to get the # before the comma:

line.find(regex)
line:gmatch(regex)
line:match(regex)

或Lua的拆分功能

是否有人对这些/其他方法的速度进行了测试,他们可以指出这是解析的快速方法?奖励积分,如果您可以说说解析小文件与大文件的速度.

Has anyone run test for speed for these/other methods which they could point out as the fast way to parse? Bonus points if you can speak to speeds for parsing small vs. large files.

推荐答案

您可能想使用line:match("%d+").

line:find也可以,但返回的结果超出了您的期望.

line:find would work as well but returns more than you want.

line:gmatch并不是您所需要的,因为它意在匹配字符串中的多个项目,而不仅仅是一个,并且要在循环中使用.

line:gmatch is not what you need because it is meant to match several items in a string, not just one, and is meant to be used in a loop.

关于速度,您必须进行自己的测量.从下面的简单代码开始:

As for speed, you'll have to make your own measurements. Start with the simple code below:

for line in io.lines("example.txt") do
  local x=line:match("%d+")
  if x~=nil then print(x) end
end

这篇关于在Lua中解析文件的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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