# Extracting values from consecutive lines

**URL:** <https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455>\
**Category:** ruby-talk\
**Created:** [17 October 2007 16:03 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455 "2007-10-17T16:03:21Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptiste\_Auguie](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@baptiste\_Auguie](https://rubytalk.org/u/baptiste_Auguie)\
**Post date:** [17 October 2007 16:03 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/1 "2007-10-17T16:03:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [17 October 2007 16:25 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/2 "2007-10-17T16:25:03Z")

</div>

# 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|  
&nbsp;&nbsp;line.scan( /\S+/ ).map{ |str| str.to\_f }  
}

my\_range = values.select{ |angle, \_|  
&nbsp;&nbsp;(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 Oct 17, 10:03 am, baptiste Auguié \<ba...@exeter.ac.uk\> wrote:
> 
> > \> 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?

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [17 October 2007 16:32 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/3 "2007-10-17T16:32:34Z")

</div>

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.

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [17 October 2007 18:10 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/4 "2007-10-17T18:10:05Z")

</div>

p IO.readlines( 'input.dat').grep( /^\d/ ).  
&nbsp;&nbsp;map{|s| s.strip.split.map{|x| x.to\_f}}.select{|a|  
&nbsp;&nbsp;&nbsp;&nbsp;a.first.between?(158,165) }

> **···**
>
> On Oct 17, 11:03 am, baptiste Auguié \<ba...@exeter.ac.uk\> 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

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [17 October 2007 18:35 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/5 "2007-10-17T18:35:20Z")

</div>

Hi,

> > 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?

Anybody considered ranges?

&nbsp;&nbsp;r = 158..165  
&nbsp;&nbsp;file.each { |l|  
&nbsp;&nbsp;&nbsp;&nbsp;ang, val1, val2 = l.split.map { |x| Float x }  
&nbsp;&nbsp;&nbsp;&nbsp;r === ang and do\_sth\_with val1, val2  
&nbsp;&nbsp;}

&nbsp;&nbsp;r = 158..165  
&nbsp;&nbsp;file.each { |l|  
&nbsp;&nbsp;&nbsp;&nbsp;ang, val1, val2 = l.split.map { |x| Float x }  
&nbsp;&nbsp;&nbsp;&nbsp;case ang  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when r then do\_sth\_with val1, val2  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}

Does anybody remember flip-flops?

&nbsp;&nbsp;file.each { |l|  
&nbsp;&nbsp;&nbsp;&nbsp;ang, val1, val2 = l.split.map { |x| Float x }  
&nbsp;&nbsp;&nbsp;&nbsp;iang = ang.floor  
&nbsp;&nbsp;&nbsp;&nbsp;if (iang==158)..(iang==165) then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do\_sth\_with val1, val2  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}

The floor call is not actually beautiful here but always  
keep in mind equality shouldn't be tested for floats.  
Flip-flops need equality.

Bertram

> **···**
>
> Am Donnerstag, 18. Okt 2007, 01:03:21 +0900 schrieb baptiste Auguié:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [17 October 2007 22:00 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/6 "2007-10-17T22:00:12Z")

</div>

p IO.readlines( 'input.dat').grep( /^\d/ ).  
&nbsp;&nbsp;map{|s| s.strip.split.map{|x| x.to\_f}}.select{|a|  
&nbsp;&nbsp;&nbsp;&nbsp;a.first.between?(158,165) }

> **···**
>
> On Oct 17, 11:03 am, baptiste Auguié \<ba...@exeter.ac.uk\> 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

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [18 October 2007 17:20 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/7 "2007-10-18T17:20:03Z")

</div>

#!awk -f  
/^158/, /^165/ { count++  
&nbsp;&nbsp;for (i=1; i\<=NF; i++)  
&nbsp;&nbsp;&nbsp;&nbsp;a[count, i] = $i + 0  
}  
END {  
&nbsp;&nbsp;for (i=1; (i,1) in a; i++)  
&nbsp;&nbsp;{ for (j=1; (i,j) in a; j++)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf "%f ", a[i,j]  
&nbsp;&nbsp;&nbsp;&nbsp;print  
&nbsp;&nbsp;}  
}

> **···**
>
> On Oct 17, 11:03 am, baptiste Auguié \<ba...@exeter.ac.uk\> 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

---

<div class="post-metadata">

**Author:** ![baptiste\_Auguie](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@baptiste\_Auguie](https://rubytalk.org/u/baptiste_Auguie)\
**Post date:** [17 October 2007 17:04 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/8 "2007-10-17T17:04:54Z")

</div>

thanks so much!

I'll try and use this array with rgsl now...

Best regards,

baptiste

> **···**
>
> On 17 Oct 2007, at 17:25, Phrogz wrote:
> 
> > On Oct 17, 10:03 am, baptiste Auguié \<ba...@exeter.ac.uk\> wrote:
> > 
> > > > 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?
> > 
> > # 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|  
> > &nbsp;&nbsp;line.scan( /\S+/ ).map{ |str| str.to\_f }  
> > }
> > 
> > my\_range = values.select{ |angle, \_|  
> > &nbsp;&nbsp;(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]]
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
> 
> Baptiste Auguié
> 
> Physics Department  
> University of Exeter  
> Stocker Road,  
> Exeter, Devon,  
> EX4 4QL, UK
> 
> Phone: +44 1392 264187
> 
> [http://newton.ex.ac.uk/research/emag](http://newton.ex.ac.uk/research/emag)  
> [http://projects.ex.ac.uk/atto](http://projects.ex.ac.uk/atto)  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [17 October 2007 17:32 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/9 "2007-10-17T17:32:12Z")

</div>

Gavin Kistner 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|  
> &nbsp;&nbsp;line.scan( /\S+/ ).map{ |str| str.to\_f }  
> }
> 
> my\_range = values.select{ |angle, \_|  
> &nbsp;&nbsp;(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'  
stop = '\> 166.00'  
results =   
get\_line = false

File.foreach("data.txt") do |line|  
&nbsp;&nbsp;test\_field = line[0, start.length]

&nbsp;&nbsp;if test\_field == start  
&nbsp;&nbsp;&nbsp;&nbsp;get\_line = true  
&nbsp;&nbsp;end

&nbsp;&nbsp;if get\_line  
&nbsp;&nbsp;&nbsp;&nbsp;results \<\< line.split()[1..-1].map{|str| str.to\_f}

&nbsp;&nbsp;&nbsp;&nbsp;if test\_field == stop  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;end  
end

> **···**
>
> > On Oct 17, 10:03 am, baptiste Augui� \<ba...@exeter.ac.uk\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [17 October 2007 19:06 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/10 "2007-10-17T19:06:00Z")

</div>

William James wrote:

> p IO.readlines( 'input.dat').grep( /^\d/ ).  
> &nbsp;&nbsp;map{|s| s.strip.split.map{|x| x.to\_f}}.select{|a|  
> &nbsp;&nbsp;&nbsp;&nbsp;a.first.between?(158,165) }

Better. Still slower.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [18 October 2007 06:10 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/11 "2007-10-18T06:10:02Z")

</div>

William James wrote:

> **···**
>
> > On Oct 17, 11:03 am, baptiste Auguié \<ba...@exeter.ac.uk\> 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
> > 
> > p IO.readlines( 'input.dat').grep( /^\d/ ).  
> > &nbsp;&nbsp;map{|s| s.strip.split.map{|x| x.to\_f}}.select{|a|  
> > &nbsp;&nbsp;&nbsp;&nbsp;a.first.between?(158,165) }
> 
> Please pardon my perverse prolixity. I threw in a "strip" even though  
> my subconscious told me it was superfluous.
> 
> p IO.readlines( 'input.dat').grep( /^\d/ ).  
> &nbsp;&nbsp;map{|s| s.split.map{|x| x.to\_f}}.select{|a|  
> &nbsp;&nbsp;&nbsp;&nbsp;a.first.between?(158,165) }

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [18 October 2007 17:33 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/12 "2007-10-18T17:33:16Z")

</div>

Hi --

> > > 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?
> 
> # 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 }  
> }

Or:

&nbsp;&nbsp;&nbsp;require 'scanf'  
&nbsp;&nbsp;&nbsp;values = IO.readlines('input.dat').map {|line| line.scanf("%f%f%f") }

(Assuming the \> at the beginning isn't really part of it -- otherwise  
"\>%f%f%f".)

I'm not entering the speed contest 🙂 but scanf might be nice for the  
conversions.

David

> **···**
>
> On Thu, 18 Oct 2007, Phrogz wrote:
> 
> > On Oct 17, 10:03 am, baptiste Auguié \<ba...@exeter.ac.uk\> wrote:
> 
> --  
> Upcoming training from Ruby Power and Light, LLC:  
> &nbsp;&nbsp;&nbsp;\* Intro to Ruby on Rails, Edison, NJ, October 23-26  
> &nbsp;&nbsp;&nbsp;\* Advancing with Rails, Edison, NJ, November 6-9  
> Both taught by David A. Black.  
> See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [18 October 2007 22:12 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/13 "2007-10-18T22:12:32Z")

</div>

William James wrote:

> #!awk -f  
> /^158/, /^165/ { count++  
> &nbsp;&nbsp;for (i=1; i\<=NF; i++)  
> &nbsp;&nbsp;&nbsp;&nbsp;a[count, i] = $i + 0  
> }  
> END {  
> &nbsp;&nbsp;for (i=1; (i,1) in a; i++)  
> &nbsp;&nbsp;{ for (j=1; (i,j) in a; j++)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf "%f ", a[i,j]  
> &nbsp;&nbsp;&nbsp;&nbsp;print  
> &nbsp;&nbsp;}  
> }

baptiste Auguié wrote:

> How can I do that in Ruby?

If that wasn't clear enough for you, here's another clue: look at the  
title of the group you posted to. Does it say 'awk' anywhere?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [18 October 2007 02:31 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/14 "2007-10-18T02:31:26Z")

</div>

# William James wrote:  
# \> p IO.readlines( 'input.dat').grep( /^\d/ ).  
# \> map{|s| s.strip.split.map{|x| x.to\_f}}.select{|a|  
# \> a.first.between?(158,165) }

> **···**
>
> From: 7stud -- [[mailto:bbxx789\_05ss@yahoo.com](mailto:bbxx789_05ss@yahoo.com)]  
> #  
> # Better. Still slower.
> 
> how about this,
> 
> a=[]  
> ARGF.each do |line|  
> &nbsp;&nbsp;if line =~ /158/ .. line =~ /165/  
> &nbsp;&nbsp;&nbsp;&nbsp;a \<\< line.split.map{|x|x.to\_f}  
> &nbsp;&nbsp;end  
> end
> 
> 'just joining the fun in ruby 🙂  
> kind regards -botp

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [18 October 2007 07:19 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/15 "2007-10-18T07:19:37Z")

</div>

William James wrote:

> William James wrote:
> 
> > \>  
> > \> \> 166.00 2.5714E-03 1.7597E-03  
> > p IO.readlines( 'input.dat').grep( /^\d/ ).  
> > &nbsp;&nbsp;map{|s| s.strip.split.map{|x| x.to\_f}}.select{|a|  
> > &nbsp;&nbsp;&nbsp;&nbsp;a.first.between?(158,165) }
> 
> Please pardon my perverse prolixity. I threw in a "strip" even though  
> my subconscious told me it was superfluous.
> 
> p IO.readlines( 'input.dat').grep( /^\d/ ).  
> &nbsp;&nbsp;map{|s| s.split.map{|x| x.to\_f}}.select{|a|  
> &nbsp;&nbsp;&nbsp;&nbsp;a.first.between?(158,165) }

I took out your superfluous grep() too, but it doesn't help your cause  
enough.

Peña, Botp wrote:

> how about this,
> 
> a=  
> ARGF.each do |line|  
> &nbsp;&nbsp;if line =~ /158/ .. line =~ /165/  
> &nbsp;&nbsp;&nbsp;&nbsp;a \<\< line.split.map{|x|x.to\_f}  
> &nbsp;&nbsp;end  
> end
> 
> 'just joining the fun in ruby 🙂  
> kind regards -botp

A winner by a nose. 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [18 October 2007 20:50 UTC](https://rubytalk.org/t/extracting-values-from-consecutive-lines/41455/16 "2007-10-18T20:50:16Z")

</div>

> Gavin Kistner 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|  
> &nbsp;&nbsp;test\_field = line[0, start.length]
> 
> &nbsp;&nbsp;if test\_field == start  
> &nbsp;&nbsp;&nbsp;&nbsp;get\_line = true  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;if get\_line  
> &nbsp;&nbsp;&nbsp;&nbsp;results \<\< line.split()[1..-1].map{|str| str.to\_f}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if test\_field == stop  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;end  
> end

Have you thought about doing all of your programming in COBOL?

> **···**
>
> On Oct 17, 12:32 pm, 7stud -- \<bbxx789\_0...@yahoo.com\> wrote:
> 
> > \> On Oct 17, 10:03 am, baptiste Augui? \<ba...@exeter.ac.uk\> wrote:
