From: John Joyce Date: 2007-10-18T01:32:34+09:00 Subject: Re: extracting values from consecutive lines On Oct 17, 2007, at 11:03 AM, baptiste Auguié wrote: > > Hello, > > This is a basic question, as I'm very new to Ruby. I have a text > file 'input.dat' containing the following data and text: > > >> some text >> some text... >> >> angle value1 value2 >> >> 156.00 3.3688E-03 1.7040E-03 >> 157.00 3.2919E-03 1.7118E-03 >> 158.00 3.2140E-03 1.7190E-03 >> 159.00 3.1354E-03 1.7258E-03 >> 160.00 3.0560E-03 1.7320E-03 >> 161.00 2.9760E-03 1.7378E-03 >> 162.00 2.8956E-03 1.7431E-03 >> 163.00 2.8148E-03 1.7479E-03 >> 164.00 2.7338E-03 1.7523E-03 >> 165.00 2.6526E-03 1.7562E-03 >> 166.00 2.5714E-03 1.7597E-03 >> 167.00 2.4902E-03 1.7628E-03 > > > I would like to extract the 3 columns of data for several > consecutive lines (say, from angle = 158 to 165). These values > should be stored in some array or vector. How can I do that in Ruby? > > Many thanks, > > baptiste > > > > Many ways to approach it of course. You could use CSV or FasterCSV since your data file is lines (rows) with columns separated by tabs or spaces. CSV or FasterCSV might be more useful to do other things also with the data file. but you can also just read the first 3 or 4 bytes of each line to find the lines you want.