From: "Jesús Gabriel y Galán" Date: 2010-08-05T15:15:53+09:00 Subject: Re: my script just read one line? On Wed, Aug 4, 2010 at 11:22 PM, Junhui Liao wrote: > Hi, Jesus, > >> You could read the lines one by one as you are doing now, but instead >> of writing them to each file on each step, accumulate them in arrays. >> For example you could have an array that contains and array of lines >> for each file. Then when you've read the whole file, iterate through >> the array writing each subarray to a file. >> > > I tried to use the following code to make all of the line's data > containing in an array. > But my code just read the last line of file. Could you please tell my > why? > >  File.open("../data/test_2lines.tsv") do |file| >  total_data = []# Array containing all of the line_datas >  line_data = [] # Array just containing one line's data You don't need to declare the variable. You will only use it inside the loop and assign it a value, so you should remove the above line. >  first_line = file.readline.chomp.split("\t") >  first_line_times = first_line.each_slice(2).map{|time,signal| time} >  file.each_line do |record| >   line_data = record.chomp.split("\t")# write all time and sginal into array line_data. >  end The problem is here: you are reading the full file splitting each line, but the result of the split is thrown away. You don't do anything with line_data in each iteration. >  total_data = total_data + line_data# append line_data into total_data After the loop, you use the last line_data, which contains the last line of the file. BTW, it's better to do total_data << line_data, because the way you are doing it, you are creating intermediate arrays that you don't need. This is how I'd do it (untested, just an idea): File.open("../data/test_2lines.tsv") do |file| total_data = Array.new(4096) {[]} # Array containing an array for each file we will generate first_line = file.readline.chomp.split("\t") first_line_times = first_line.each_slice(2).map{|time,signal| time} file.each_line do |record| record.chomp.split("\t").each_slice(2).with_index do |(time,signal), index| total_data[index] << "#{time.to_f - first_line_times[index].to_f}\t#{signal}\n" end end total_data.each_with_index do |lines, index| File.open("header_split_#{index}"+".tsv" , "w") do |f|   f.puts total_data[index] end end Hope this helps, Jesus.