From: William James Date: 2007-10-19T05:50:16+09:00 Subject: Re: extracting values from consecutive lines On Oct 17, 12:32 pm, 7stud -- wrote: > Gavin Kistner wrote: > > On Oct 17, 10:03 am, baptiste Augui? wrote: > >> > 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? > > > # Make an array of arrays of numbers > > # based on scanning for non-whitespace characters > > # (all strings will show up as 0.0) > > values = IO.readlines( 'input.dat' ).map{ |line| > > line.scan( /\S+/ ).map{ |str| str.to_f } > > } > > > my_range = values.select{ |angle, _| > > (158..165).include?( angle ) > > } > > > require 'pp' > > pp my_range > > #=> [[158.0, 0.003214, 0.001719], > > #=> [159.0, 0.0031354, 0.0017258], > > #=> [160.0, 0.003056, 0.001732], > > #=> [161.0, 0.002976, 0.0017378], > > #=> [162.0, 0.0028956, 0.0017431], > > #=> [163.0, 0.0028148, 0.0017479], > > #=> [164.0, 0.0027338, 0.0017523], > > #=> [165.0, 0.0026526, 0.0017562]] > > On my system, this is faster: > > start = '> 163.00' If you can't guess that "> " isn't actually part of the data, you could have deduced it from the fact that the o.p. gave Gavin's program the seal of approval. Gavin's program won't work if the lines start with "> ". Your program won't work since the lines don't start with "> ". > stop = '> 166.00' > results = [] > get_line = false > > File.foreach("data.txt") do |line| > test_field = line[0, start.length] > > if test_field == start > get_line = true > end > > if get_line > results << line.split()[1..-1].map{|str| str.to_f} > > if test_field == stop > break > end > > end > end Have you thought about doing all of your programming in COBOL?