From: Jesús Gabriel y Galán on 5 Aug 2010 02:15 On Wed, Aug 4, 2010 at 11:22 PM, Junhui Liao <junhui.liao(a)uclouvain.be> 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.
From: Junhui Liao on 5 Aug 2010 18:05 > 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. Dear Jesus, I modified little, then the code worked. This new script just took 3 (THREE) minutes, it's incredible !!! Millions of thanks to you! My main reference book is <<Programing ruby_the progmatic programmers>>, I don't know why in this book, I could not find these information, like each_with_index, and like the example of two items between two bars "||", map{|time,signal| time} ? Where I can find these information ? Thanks a lot in advance! Cheers, Junhui -- Posted via http://www.ruby-forum.com/.
From: Jesús Gabriel y Galán on 6 Aug 2010 02:07 On Fri, Aug 6, 2010 at 12:05 AM, Junhui Liao <junhui.liao(a)uclouvain.be> wrote: > >> 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. > > > > Dear Jesus, > > I modified little, then the code worked. > This new script just took 3 (THREE) minutes, it's incredible !!! > Millions of thanks to you! > > My main reference book is <<Programing ruby_the progmatic programmers>>, Are you referring to this one: http://www.ruby-doc.org/docs/ProgrammingRuby/ > I don't know why in this book, I could not find these information, like > each_with_index, Well, I don't know if it's mentioned in the text, but there's a reference: http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_enumerable.html#Enumerable.each_with_index > and like the example of two items between two bars "||", > map{|time,signal| time} ? > Where I can find these information ? ... but I recommend you take a look at the API reference here: http://ruby-doc.org/core/ and study the classes: Array, Hash and Enumerable, which are pretty important to master (of course there are others like String and so on...) Jesus.
From: Junhui Liao on 6 Aug 2010 06:25
Hi, Jesus, > Are you referring to this one: > http://www.ruby-doc.org/docs/ProgrammingRuby/ > >> I don't know why in this book, I could not find these information, like >> each_with_index, It is this book. And I made a mistake, in this book (I bought a PDF version), there exists the each_with_index. But I did search "each_with_index" in this book before but failed. Maybe there was a typing mistake when searching. > Well, I don't know if it's mentioned in the text, but there's a > reference: > > http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_enumerable.html#Enumerable.each_with_index > >> and like the example of two items between two bars "||", >> map{|time,signal| time} ? >> Where I can find these information ? > ... but I recommend you take a look at the API reference here: > > http://ruby-doc.org/core/ > > and study the classes: Array, Hash and Enumerable, which are pretty > important to master (of course there are others like String and so > on...) Thanks a lot for all of these references ! Cheers, Junhui -- Posted via http://www.ruby-forum.com/. |