0
我試圖從CSV文件中選出最佳的3個結果。讀取文件沒有問題,但我無法從我的文件中選擇最佳結果。ruby讀取文件並選擇最佳結果
這是我迄今爲止接從文件中最好的3個結果:
CSV文件:
Brain 345677
Mary 5
John 23355676
William 432200
Jessica 21
Ruby代碼:
elements = []
file = File.open'ballots.csv', 'r' do |file|
header = false
ballots = []
results = []
while line = file.gets
unless header
element = line.split ','
if [:ballots1] > [:ballots2]
results = ballots1
end
else
results = ballots2
end
puts "#{element[3]}:#{element[4]}"
end
header = false
end
應該有3最好的,不只是一個最大 –
@YevgeniyAnfilofyev權,我已經爲'require'csv''更新了我的回答 – Stefan
+1。好的代碼通常是懶惰的代碼,寧願讓別人去做這件事。 –