From: Phil Rhoades Date: 2008-01-11T07:21:05+09:00 Subject: Re: Multiline (block) CSV file processing Brian, On Fri, 2008-01-11 at 06:47 +0900, Phil Rhoades wrote: > Brian, > > > On Fri, 2008-01-11 at 04:19 +0900, Brian Adkins wrote: > > On Jan 10, 1:40 pm, Brian Adkins wrote: > > > On Jan 10, 11:18 am, Phil Rhoades wrote: > > > > > > > Thanks but not quite - say my input file is: > > > > > > > 1 2 3 4 5 6 7 8 9 a b c > > > > 11 12 13 14 15 16 17 18 19 d e f > > > > 21 22 23 24 25 26 27 28 29 g h i > > > > 31 32 33 34 35 36 37 38 39 j k l > > > > 41 42 43 44 45 46 47 48 49 m n o > > > > > > > The output should be: > > > > > > > 1 12 25 j o > > > > This is a little more general. > > > > 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 . . Also, to generalise the code further, how would you select two of more fields from each line? Thanks, Phil. -- Philip Rhoades Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275) GPO Box 3411 Sydney NSW 2001 Australia Fax: +61:(0)2-8221-9599 E-mail: phil@pricom.com.au