我有一个包含多个条目的文本文件。格式是固定的,是name_version_versionNumber
.
**example.txt**
cool_name_A_001
something_else_WIP_002
something_else_001
cool_name_B_002
other_thing_010
other_thing_006
退货清单应消除 WIP 条目,并提供最新(最高数量)版本。上面文本文件的输出应该是
cool_name_A_001
cool_name_B_002
other_thing_010
something_else_001
到目前为止,我有
#import files
x = File.readlines("path to txt file").delete_if {|x| x.scan(/[WIP]../).include? "WIP"}.sort
#include latest copy only
#include latest copy only
def latest_version(x)
list = []
i = 0
while i<x.length
if list.map {|x| x.scan(/\D+/)}.flatten.include? x[i].scan(/\D+/)
zet = list.map {|x| x.scan(/\D+/)}.flatten.rindex x[i].scan(/\D+/)
if list[zet].scan(/\d+/) > x[i].scan(/\d+/)
i+=1
else
list[zet] = x[i]
i+=1
end
elsif x[i].scan(/\D+/) == x[i+1].scan(/\D+/)
if x[i].scan(/\d+/) > x[i+1].scan(/\d+/)
list << x[i]
i+=1
else
list << x[i+1]
i+=1
end
else
list << x[i]
i+=1
end
end
list
end
puts latest_version(x)
我收到以下错误
rb:10:in `latest_version': private method `scan' called for 114:Fixnum (NoMethodError)
error 方法在 irb 中有效,无法弄清楚为什么会出错?此外,无法判断逻辑是否实现了预期的结果。请帮忙!谢谢 :)
1.9.3p374 :098 > y
=> ["something_SA_R33\n", "whatever_SA_R012\n", "anything_SB_R012\n"]
1.9.3p374 :099 > y.map {|x| x.scan(/\d+/)}.flatten
=> ["33", "012", "012"]