L
Li Chen
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
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