我有記錄的txt文件:ruby數組可以each_with_index做步嗎?
firstname lastname dob ssn status1 status2 status3 status4 firstname lastname dob ...
我能進一個數組這樣的:
tokens[0] = firstname
...
tokens[8] = firstname (of record 2).
tokens[9] = lastname (of record 2) and so on.
我想遍歷tokens
陣列中的步驟,這樣我就可以說:
record1 = tokens[index] + tokens[index+1] + tokens[index+2] etc.
和步驟(在上面的例子8中)將處理記錄:
record2, record3 etc etc.
step 0 index is 0
step 1 (step set to 8 so index is 8)
etc.
我想我應該說,這些記錄是從我叫.split一個txt文件來:
file = File.open(ARGV[0], 'r')
line = ""
while !file.eof?
line = file.readline
end
##knowing a set is how many fields, do it over and over again.
tokens = line.split(" ")
你能重新編輯你的代碼?也給你java代碼和期望的輸出,它在Java中看起來如何。可能這會很好的回答你的速度。 –