2013-03-18 105 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

相關問題