From: Junhui Liao Date: 2010-07-29T23:04:52+09:00 Subject: Re: my script just read one line? > You are adding 2 to the counter every iteration, but not clearing it > after every line. So, on the second line, counter will still be 4096, > and so you will try to get an element from the array that is out of > bounds, returning nil and raising the NoMethodError, because you are > calling the + method on nil. I think you are complicated the issue > with the counting and so on, usually the Ruby iterators are a cleaner > way to traverse lists of things. You can remove the use of > itemnum,counter and so on like this (untested): Many thanks for your comment ! > File.open("../original_data/test_2lines.tsv").each_line do |record| > a = record.chomp.split("\t") > a.each_slice(2).with_index do |(time,signal), index| > File.open("#{index}_debug_split"+".tsv" , "w") do |f| > f << "#{time}\t#{signal}\n" > end > end > end I tried the script, but added "require 'enumerator' ". Still, there is a problem like this : new_split_Jesus.rb:1:in `each_slice': no block given (LocalJumpError) After looking for this forum, I got that this results from my mac based ruby is 1.8.6, and your code should worked under 1.9 + . Even though I don't know how to do "requires a block to be passed to it" Refer to this link please: http://www.ruby-forum.com/topic/201095#new > Although this will open and close the 4096 files for every line. Are > there many lines? If not, you can read the whole file and build a > structure in memory (a hash of arrays) to store the lines that belong > to every file, and then write them at once to each file. Yes, my file is totally 2048 lines, ~260M. So, if read the whole file into memory, the efficiency maybe not so nice. Thanks again for your help ! Best, Junhui -- Posted via http://www.ruby-forum.com/.