2013-04-11 70 views
0

以下是我的数组中的项目列表:如果看到有生成的日期文件(例如,update.20130410.xml)。我应该只采取最新的文件,并删除其余的。让我们假设数组的值是排序的,所以如果我们只是在重复模式中选择最上面的项目,那应该没问题。请帮帮我。Ruby排序和删除数组中的重复项

原始数组:

theOneBig.xml 
theSecondFine.xml 
theTestOne.xml 
OtherImp.xml 
mrss.xml 
update.20130410.xml 
update.20130402.xml 
update.20120314.xml 
update.20120313.xml 
update.20120312.xml 
update.20120301.xml 
update.20130411.xml 
file.update.20130410.xml 
file.update.20130409.xml 
file.update.20130409.xml 

输出我想获得是

theOneBig.xml 
theSecondFine.xml 
theTestOne.xml 
OtherImp.xml 
mrss.xml 
update.20130410.xml 
file.update.20130410.xml 
+2

什么是“最新文件”?删除重复项时输出的内容是什么? – Danny 2013-04-11 14:47:49

+0

我更新了帖子,我想如何得到我的最终结果 – Krish 2013-04-11 15:06:17

回答

1

假设列表已经排序正确,日期总是开始,以期结束,你永远不有一个以不是的日期开始和结束的数字序列,那么这应该工作(对于Ruby 1.9.3):

original_array.uniq {|f| f.gsub(/(?<=\.)\d+(?=\.)/, '') } 

当您提供Array#uniq的块时,它会使用返回值来确定“唯一性”。

+0

感谢Wally的回答,但我得到了SyntaxError:编译错误 (irb):20:undefined(?...)sequence:/(?<= \。 )\ d +(?= \。)/ – Krish 2013-04-11 16:02:06

+0

它适用于Ruby 1.9.3,但在使用1.8.7时出现同样的错误。 – 2013-04-11 16:09:41

+0

1.8.7正则表达式没有前瞻/后面 – 2013-04-11 16:27:28