2013-03-18 137 views
1

我需要在openssl的index.txt文件中查找特定记录,然后根据时间戳对它们进行排序,以便最新的记录出现在顶部。我把文件读入一个数组,数组是这样的:(我甩表到一个日志文件)如何根据时间戳对openssl的index.txt进行排序

database= {} 
database[1] = "R 140318001552Z 130318002148Z 7D  unknown /[email protected]/[email protected]" 
database[2] = "V 140318001809Z   7E  unknown /[email protected]/[email protected]" 
database[3] = "V 140318002157Z   7F  unknown /[email protected]/[email protected]" 

这个我想能够通过第二到这个数组排序列,这是时间戳。我需要将最新的记录先排序。 我如何在lua中编写此代码?

谢谢。

回答

1

如果你的图案是这样修复的,你可以简单地搜索第一个数字(使用string.match)并比较这些数字。请注意,string.match会给你的字符串不是数字。但由于这些数字的长度相同,所以词汇比较就足够了。当然,如果需要的话,您可以使用适当的库将该字符串转换为数字或日期/时间对象。但是,让我们保持它的简单:

table.sort(database, function(e1,e2) 
    return string.match(e1, "%d+") > string.match(e2, "%d+") 
end) 

提供给sort函数应该返回true如果e1应在排序表来e2之前。

+0

你介意扩展你的答案,包括代码来做string.match你提到?我对卢阿很新。谢谢 – dot 2013-03-18 01:36:48

+0

@dot'string.match'是一个标准的库函数。只需在您的lua文件的开始处输入“require”字符串“'”。参见[here](http://www.lua.org/manual/5.2/manual.html#pdf-string.match)的文档和[here](http://www.lua.org/manual/5.2) /manual.html#6.4.1)了解模式的工作方式。 – 2013-03-18 08:30:55

相关问题