Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > problem with reading file

Reply
Thread Tools

problem with reading file

 
 
Li Chen
Guest
Posts: n/a
 
      10-20-2006
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



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Wilson Bilkovich
Guest
Posts: n/a
 
      10-20-2006
On 10/20/06, Li Chen <(E-Mail Removed)> wrote:
> 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
>


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

 
Reply With Quote
 
 
 
 
Li Chen
Guest
Posts: n/a
 
      10-20-2006

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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-20-2006
On 20.10.2006 17:25, Li Chen wrote:
> 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


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
ASP.NET Reading problem (reading .xls) Wael Soliman ASP .Net 2 01-03-2005 05:33 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments