From: "David A. Black" Date: 2009-06-24T00:10:55+09:00 Subject: Re: formatting a listing On Tue, 23 Jun 2009, George George wrote: > @David Black: > My input consists of digits and not letters, that is why i used digits > in the examples above; in short what i really wanted is: Given a listing > 1 > 2 > 3 > white space > 3.05 > 4 > 5 > whitespace > -6.3 > 7 > > Produce an output such that: > 1,-3.05,-6.3 That's a spurious - before the 3, right? > 2,4,7 > 3,5 > > or > 1,-3.05,-6.3 > 2,4,7 > 3,5,0 The example that I couldn't quite figure out was the one with input like: 1 2,3 4 or something like that. Anyway, a simple version (which may not handle that case) is: $/ = "\n\n" # if the input is definitely \n\n delimited cols = File.open("input.csv") do |fh| max = 0 fh.map do |s| row = s.scan(/\S+/) max = [max, row.size].max row << "0" until row.size == max row end end David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com