f = File.open("faq06.tsv","r:utf-8") categories = Hash.new{|hash, key| hash[key] = Array.new} #hash of arrays of arrays (lines) f.each_line.with_index do |line, index| if index > 0 line1 = line.strip.split("\t") categories[line1[0]] << line1#[1..-1] end end o = File.open("faq_mismatched.tsv","w:utf-8") o.puts "category_id\tsource\tcategory\tquestion\tcandidate_answer\tcorrect_answer\tlink" categories.each_pair do |category, array| #nquestions = linearray.length/4 #if nquestions > 0 #end nquestions = array.length if nquestions > 1 shuffled_answers = [] array.each do |linearray| shuffled_answers << linearray[6] end shuffled_answers.shuffle! array.each.with_index do |linearray, index| outputline = "#{linearray[0]}\t#{linearray[2]}\t#{linearray[4..5].join("\t")}\t#{shuffled_answers[index]}\t#{linearray[6]}\t#{linearray[3]}" o.puts outputline end end end