From: Brian Adkins Date: 2008-01-12T13:20:01+09:00 Subject: Re: Multiline (block) CSV file processing On Jan 10, 5:21 pm, Phil Rhoades wrote: > On Fri, 2008-01-11 at 06:47 +0900, Phil Rhoades wrote: > > On Fri, 2008-01-11 at 04:19 +0900, Brian Adkins wrote: > > > def block_extractor file, fields, > > > splitter = lambda {|line| line.split } > > > while !file.eof > > > result = [] > > > fields.each do |field| > > > line = file.readline > > > result << splitter.call(line.chomp)[field-1] if field > > > end > > > yield result > > > end > > > end > > > > File.open("data.txt", "r") do |file| > > > block_extractor(file, [1,2,5,10,12]) do |fields| > > > puts fields.join(' ') > > > end > > > end > > > Thanks! - now I just need to work out how that actually works and then > > work out how I can modify it to use command line parameters. > > I apologise for replying to my own post but I have had a look at this > and read up about Procs and Lambdas and I can sorta see what you are > doing but would you be so kind as to elaborate on the code a bit? - I > think other people would find it useful as well . . I'd be glad to. What question do you have? > Also, to generalise the code further, how would you select two of more > fields from each line? Well, this is very much a toy/example program, so I wouldn't build on it too much. There is a cost and a benefit to generalization, so it might be worthwhile to spend some time thinking about how general you need the function to be. A simple way to select two or more fields from each line would be to change from: [a, b, ...] to: [ [a1, a2, ...] [b1, b2, ...] ... ] or possibly use a hash with the key being a block-relative line number, and the value being a list of field numbers. Or you may want an external specification of the fields to extract - kind of like HTML templating in reverse. Brian Adkins