From: Ruby Kk Date: 2009-03-07T11:38:55+09:00 Subject: Re: text processing Hi there, I am new to ruby and trying to create a flat file with fixed column length. i have a header,footer and records that i want in the file. any ideas? Cheers! Gary Wright wrote: > On Mar 30, 2007, at 3:31 PM, Stephen Smith wrote: >> {9})/.match(line).captures.join(',') >> << "\n" >> newLogg << record >> record = "" >> end > > How about making it so you don't have to type that pattern next time: > > def columns(*widths) > Regexp.new(widths.map {|count| "(.{#{count}})" }.join) > end > pattern = columns(6,15,3,30,4,15,1,12,9) > > You also can get rid of the intermediate strings and the potentially > huge > internal array (from slurping up your oldLog into an array): > > oldLog.each { |line| > newLog.print pattern.match(line).captures.join(',') > } -- Posted via http://www.ruby-forum.com/.