0
我有一個連續的數據流,由整數組成,我需要添加它們以將三個組添加到數據庫。 我一直在Google上搜索一兩天,但還沒有找到可行的解決方案。添加三個int tp數組
所以我問這個大腦來幫助我。
def index
i=0
speedData = []
tempArray = []
File.open("/var/www/html/speeds.txt", "r").each_line do |line|
speedData.push(line.split(/\s/)[1].to_f.round)
puts speedData.last
if i<3
tempArray.push(speedData.last)
i=+1
end
puts tempArray.inspect
#Speed.create(:ping => tempArray[0], :down => tempArray[1], :up => tempArray[2])
i=0
tempArray.clear
end
end
puts i
end
這是我當前的解決方案...不工作,不介意你。 :) 那麼我該怎麼辦? groups_of看起來不錯,但...
謝謝!這是一個正確的方向格式speeds.txt是這樣的:Ping:35.791 ms 下載:26.84 Mbit/s 上載:4.63 Mbit/s –
@swe_mattias你可能想使用regexps然後:'line =「Ping :35.791 ms下載:26.84 Mbit/s上載:4.63 Mbit/s「;匹配=/Ping:(? \ d + \。\ d +)。+?下載:(? \ d + \。\ d +)。+?上傳者:(? \ d + \。\ d +)/ );把[match [:up],match [:down],match [:ping]]放在''上 –
faron