From: "David A. Black" Date: 2007-11-15T07:06:47+09:00 Subject: Re: Recursion help. Hi -- On Thu, 15 Nov 2007, JeremyWoertink@gmail.com wrote: > I suck at recursion, and I need some help understanding where I'm > going wrong here. > > I have a script that reads a file, then creates an excel form file. > Here's the program. > > puts "Enter file name" > filename = gets.chomp > > puts "Enter carton size" > carton_size = gets.chomp > > out_file = File.new("C:/test/inner workings.xls", "w+") > out_file.write("Carton number \t Carton quantity \t Card number \n") > > file_size = IO.readlines(filename).size > total_cartons = (file_size.to_f / carton_size.to_f).ceil > i = 0 > sequence = 1 > > in_file = File.open(filename) > > unless in_file.eof? > > (i..(carton_size.to_i - 1)).each do |number| > record = in_file.readline > out_file.write("#{sequence}\t#{total_cartons}\t#{record[7, 19]} > \n") > end > > sequence += 1 > if sequence > total_cartons > break > end > end > > > The problem I am running into, is it works fine until the sequence += > 1 part. > the output looks like > 1 \t 7 \t 1234 1234 1234 > 1 \t 7 \t 1234 1234 1235 > > then right before > 2 \t 7 \t 1234 1234 1300 > it says "Done!" and closes. > > out_file.close > puts "Done!" > > > > I hope this makes sense enough for me to get some help. Do you want 'until' instead of 'unless'? David -- Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing With Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details!