# Problem with reading file

**URL:** <https://www.ruby-forum.com/t/problem-with-reading-file/73014>\
**Category:** Ruby\
**Created:** [October 20, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/problem-with-reading-file/73014 "2006-10-20T17:25:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/problem-with-reading-file/73014/1 "2006-10-20T17:25:18Z")

</div>

Hi folks,

I read a file in the following format,extract the second column only,  
and push them into an array. My problem is that Ruby reads them as  
strings(based on array.inspect) but not as numbers so it is impossible  
for me to do the calculation. Any comments?

Thanks,

Li

###filen name: array.text

A 1 B  
A 2 B  
A 3 B  
A 4 B  
A 5 B  
A 6 B

…

###script

equire ‘enumerator’

array=Array.new

File.open(‘array.text’).each{|line| array\<\<line.split(/\t/)[1]}

#puts array.inspect  
array.each\_slice(3){|slice|  
average=(slice[0]+slice[1]+slice[2])/(3.0)  
puts average  
}

###output from screen

C:/Ruby/self/file6.rb:8: undefined method `/' for "123":String (NoMethodError) from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`each\_slice’  
from C:/Ruby/self/file6.rb:7:in `each' from C:/Ruby/self/file6.rb:7:in`each\_slice’  
from C:/Ruby/self/file6.rb:7

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/problem-with-reading-file/73014/2 "2006-10-20T17:42:19Z")

</div>

On 10/20/06, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …  
> array.each\_slice(3){|slice|  
> `each_slice' from C:/Ruby/self/file6.rb:7:in `each’  
> from C:/Ruby/self/file6.rb:7:in `each\_slice’  
> from C:/Ruby/self/file6.rb:7

You need to tell Ruby to coerce the strings into integers.

array = []

File.open(‘array.text’) do |file|  
file.each\_line do |line|  
array \<\< line.split[1].to\_i  
end  
end

array.each\_slice(3) do |triplet|  
average = (slice[0]+slice[1]+slice[2])/3.0  
puts average  
end

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/problem-with-reading-file/73014/3 "2006-10-20T18:06:32Z")

</div>

> You need to tell Ruby to coerce the strings into integers.
> 
> array = []
> 
> File.open(‘array.text’) do |file|  
> file.each\_line do |line|  
> array \<\< line.split[1].to\_i  
> end  
> end
> 
> array.each\_slice(3) do |triplet|  
> average = (slice[0]+slice[1]+slice[2])/3.0  
> puts average  
> end

Thank you very much,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/problem-with-reading-file/73014/4 "2006-10-20T18:22:32Z")

</div>

On 20.10.2006 17:25, Li Chen wrote:

> …  
> array.each\_slice(3){|slice|  
> `each_slice' from C:/Ruby/self/file6.rb:7:in`each’  
> from C:/Ruby/self/file6.rb:7:in `each\_slice’  
> from C:/Ruby/self/file6.rb:7

This can be done by an awk style 1liner 🙂

18:12:44 [Temp]: cat data  
A 1 B  
A 2 B  
A 3 B  
A 4 B  
A 5 B  
A 6 B  
18:13:18 [Temp]: ruby -aF\t -n -r enumerator -e ‘BEGIN{$a=[]};  
$a\<\<$F[1].to\_i; END{ $a.each\_slice(3){|sl| p  
sl.inject(0){|a,b|a+b}/3.0}}’ data  
2.0  
5.0  
18:13:34 [Temp]:

Kind regards

```
robert
```
