在ruby中对stats文件进行排序

时间:2014-11-08 06:13:22

标签: ruby sorting readfile

所以这里是我需要排序的文件。

Jump067
1
4c3jump1
JUMPMOD067TOP10TIMES
 10/07/13 17.363001 259 04/10/12 17.385000 343 04/10/12 17.678001 235 05/12/12 17.685001 265 10/07/13 17.980001 210 21/10/14 18.478001 773 06/06/12 18.523001 208 10/07/13 18.592001 214 27/06/14 18.772001 349 21/10/14 18.978001 630 21/10/14 19.025002 772 10/07/13 19.240002 69 02/05/12 19.356001 219 27/06/14 19.604000 693 24/04/12 19.756001 204
JUMPMOD067ALLTIMES
 24/04/12 19.756001 204 6 02/05/12 19.356001 219 1 06/06/12 18.523001 208 1 05/12/12 17.685001 265 6 10/07/13 17.363001 259 8 05/12/12 23.172001 353 7 04/10/12 17.678001 235 2 04/10/12 17.385000 343 1 27/06/14 18.772001 349 6 05/12/12 24.316002 299 2 04/04/13 22.014002 61 2 10/07/13 17.980001 210 2 10/07/13 19.240002 69 6 10/07/13 18.592001 214 7 26/08/13 22.243002 549 4 26/08/13 22.927002 580 4 19/09/13 20.098001 579 3 26/10/13 104.303001 603 1 29/12/13 52.244003 290 1 06/03/14 28.275002 294 1 02/04/14 22.736002 406 3 27/06/14 19.604000 693 3 11/09/14 20.366001 540 2 21/10/14 18.478001 773 8 21/10/14 20.771002 714 4 21/10/14 19.025002 772 4 21/10/14 18.978001 630 2 21/10/14 21.544001 510 1

该文件用于震动模型的统计数据。格式如下:

[游戏版本] [1(总是1)] [mapname] [JUMPMOD067TOP10TIMES]

top10 times部分的格式为[date] [finish_time] [user id]

[JUMPMOD067ALLTIMES]

所有时间段的格式为[日期] [完成时间] [用户ID] [map_completions]

我需要做的是在文本文件中读取按[finish_time]的顺序对alltimes部分进行排序,删除[map_completions]数字,然后将top10times部分替换为所有时间的排序版本。这必须在大约2500个文件中完成,或者我会手工完成。任何建议或提示将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的方法,在其中将得分文件名作为命令行参数传递,并写入filename.calculated

在* nix上,您可以使用find . -name '*scores' -exec ruby calculate_high_scores.rb {} \;

在所有乐谱文件中调用它
file_name = ARGV[0]
file_content = File.read(file_name)
file_content.match /([\w\d]+TOP10TIMES).* ([\w\d]+ALLTIMES)/

top_10_label = $1.upcase
all_times_label = $2.upcase

header, scores = file_content.split(top_10_label)
top_10, all_times = file_content.split(all_times_label)

sorted = all_times.split(' ').each_slice(4).to_a.sort do |a,b|
  b[1].to_f <=> a[1].to_f
end

calculate_top_10 = sorted[0..9].map { |score| score[0..2].join(' ') }.join(' ')

result = "#{header}#{top_10_label} #{calculate_top_10} #{all_times_label}#{all_times}"

File.open("#{file_name}.calculated", "w") do |f|
  f << result
end
祝你好运!